Asp.net-MVC now allows for implicit binding of DateTime objects. I have an action along the lines of
public ActionResult DoSomething(DateTime startDate)
{
...
}
This successfully converts a string from an ajax call into a DateTime. However, we use the date format dd/MM/yyyy; MVC is converting to MM/dd/yyyy. For example, submitting a call to the action with a string '09/02/2009' results in a DateTime of '02/09/2009 00:00:00', or September 2nd in our local settings.
I don't want to roll my own model binder for the sake of a date format. But it seems needless to have to change the action to accept a string and then use DateTime.Parse if MVC is capable of doing this for me.
Is there any way to alter the date format used in the default model binder for DateTime? Shouldn't the default model binder use your localisation settings anyway?
Try to use toISOString(). It returns string in ISO8601 format.
GET method
javascript
c#
POST method
javascript
c#
I set
CurrentCulture
andCurrentUICulture
my custom base controllerIt going to be slightly different in MVC 3.
Suppose we have a controller and a view with Get method
We should add ModelBinder
and the command in Application_Start() of Global.asax