0

This is something new :)

My culture is 'nl' (dutch) and i suppose my model can't parse the string that contains DateTime -> (DateTime)formColl['CreatedOn'] doesn't work. DateTime.Parse(formcoll['CreatedOn'] works though.

Also, it's a hidden field in my view.

@Html.HiddenFor(Model => Model.CreatedOn)

How would i fix this without manually adding the conversion (i suspect this would work out of the box)?

Edit: There seems to be a difference with Chrome and Firefox. With Chrome, the conversion doesn't work and with firefox it does. My primary browserLanguage on Chrome is : 'en' (in the settings it was dutch, but on the third place :-S) My primary browserLanguage on Firefox is : 'nl'

In Chrome Empy model value : CreatedOn (Chrome) More info about chrome submitted value Filled in value in Formcollection -> Window information (Chrome) In FireFox Screenshot with Firefox

NicoJuicy
  • 3,435
  • 4
  • 40
  • 66

1 Answers1

2

(DateTime)formColl['CreatedOn'] doesn't work.

That's perfectly normal. You cannot cast a string (which is what formColl['CreatedOn'] represents) to a DateTime instance.

You may try setting the culture in your web.config in the <globalization> element to see if this makes any difference:

<globalization culture="nl-NL" uiCulture="nl-NL" />
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I use in my web.config, i'd suppose this would auto parse the datetime... It's a multi-langual app, so i think i should leave the web.config be with the "auto" attributes for globalization. – NicoJuicy Feb 26 '13 at 22:29
  • `auto` uses the browser culture to parse datetimes. So if you want this to work make sure you have configured your client browser to use Dutch as preferred language. If you have setup `en-US` as preferred on your browser this won't work because `26-2-2013` is not a valid DateTime in the `en-US` culture. – Darin Dimitrov Feb 26 '13 at 22:30
  • Weird, because my current language is "Dutch" in my browser. I'll check with another browser first to see if i have the same problem. – NicoJuicy Feb 26 '13 at 22:32
  • If on the other hand you want to have a generic solution working with predefined formats you could use the DisplayFormat attribute on your view model and then have a custom model binder which uses this format to parse the dates accordingly. I have written a sample here: http://stackoverflow.com/a/7836093/29407 – Darin Dimitrov Feb 26 '13 at 22:33
  • So i removed my other languages in the browser (Chrome), restarted it and now my primary language is 'nl' and everything is ok. Still have to find a workarround for this though, but you put me on the right track with defining auto in the web.config. Thanks! – NicoJuicy Feb 26 '13 at 22:50