Thursday, August 11, 2011

ASP.NET MVC 3 ModelBinding Localization: incorrect date

One of the great features of ASP.NET MVC is modelbinding. It automatically converts data coming from your form, route data, query string parameters etc… into parameters on your action methods. However we noticed one problem. Imagine you have the following action method

public ActionResult PlaceOrder(DateTime orderDate) 
{ 
... 
}

The problem that we noticed is that  MVC is converting our datetime to MM/dd/yyyy although our users are setting dates through the date format dd/MM/yyyy. For example, submitting a call to the action with a string '03/02/2011' results in a DateTime of  '02/03/2011'.

We were able to solve this issue by adding a globalization section to our web.config. The modelbinder picks this up and uses the correct date format.


<system.web>    
<globalization uiCulture="nl-BE" culture="nl-BE" />
</system.web>

1 comment:

Greg David said...

Hi! To speed up things with l10n projects, I would recommend to check out an online localization service like https://poeditor.com