[RESOLVED]Very strange html.hiddenfor rendering problem

@Html.HiddenFor(model => model.NextCallDue)
@Html.DisplayFor(model => model.NextCallDue)

the above markup produces:

<input data-val="true" data-val-date="The field NextCallDue must be a date." id="selectedLead_NextCallDue" name="selectedLead.NextCallDue" type="hidden" value="1/1/0001 12:00:00 AM" />

10/1/2014 2:42:42 PM

In other words, the HiddenFor puts a null value (1/1/0001 12:00:00 AM) for the same date as the DisplayFor which correctly produces: 10/1/2014 2:42:42 PM

I guess its something to do with the validation, although the date is valid in anyone’s money – but I can’t get to the validation code to see it breaking (I’m guessing…)

Please post the model code, what attributes are you using for the NextCallDue field.

As a work around you can use this

@Html.Hidden("NextCallDue", model.NextCallDue)

I’ve tried completely undecorated:

private System.DateTime _nextCallDue;
public System.DateTime NextCallDue{
    get { return _nextCallDue; }
    set { _nextCallDue = value; }
}
I've made it with an explicit backing val to see if I could break into the code when it was being used by HiddenFor, but I couldn't.
It does work incidentally if I hardcode a date e.g.
 get { return new System.DateTime(2014, 4, 5); }

Are you setting the value of NextCallDue in your controller?

I suspect DisplayFor may be initializing the date to current datetime and showing it, whereas HiddenFor is not doing the initialization

‘Fraid not, its coming out of the DAL like that, I just clicked another rec:

<input
data-val="true"
data-val-date="The
field NextCallDue must be a date."

id="selectedLead_NextCallDue"
name="selectedLead.NextCallDue"
type="hidden"
value="1/1/0001
12:00:00 AM"
/>

  

9/30/2014 2:31:02 PM

Actually, just looking at the thing I just posted made me think, its doing a US date, and my UI culture is UK, maybe that’s why its failing the Validation, which uses the currentUI culture (apparently)

I’ve just seen this link which describes the same problem:

http://stackoverflow.com/questions/15147467/mvc3-date-in-uk-format-not-received-on-controller

not sure about the resolution though.

It seems definitely the issue with culture. Have you explicitly set your culture?

Also did you try the workaround?

@Html.Hidden("NextCallDue", model.NextCallDue)

Leave a Reply