Question

I have a form on an ASP.NET MVC site which I've recently changed from POST to GET. However when I've done this I've realised that the DateTime parameters in the related action are now effectively using a different culture - en-US for GET, rather than en-GB for POST. So basically the day and month are getting switched.

The following articles have explained to me why this is done however I needed to find a way of overcoming it without reverting to POST or using JS to modify the form before the submit.

http://xhalent.wordpress.com/2011/05/14/localization-of-dates-in-asp-net-mvc/ http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

I created a custom binder for DateTime, just to trial out a theory, but in doing so it seems to have fixed the issue. Can anyone explain why this works?

The custom binder was basically the following line in the global.ascx.cs

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());

and this for the DateTimeModelBinder class

public class DateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        return bindingContext.Model;
    }
}
Was it helpful?

Solution

It seems JS is the only way to do this. What I thought was the answer above was simply returning the value set by the Model's constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top