Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What formats does ASP.NET MVC expect for DateTime so that the model binding will work properly?

I have a situation in which I need to set a date and time to a hidden field via javascript. I must be able to catch this hidden field value as a parameter of my action in the server.

The problem is that I don't know what format to use to write the datetime in the hidden field. I googled a lot and didn't find a table explaining what datetime formats are compatible with the default model binding engine.

I've tried both iso datetime: "yyyy-mm-dd'T'HH:MM:ss" and iso utc datetime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" with no success.

PS: I know ASP.NET MVC accounts for whether it's a GET or a POST while dealing with formats and cultures. My culture is PT-BR and it's a POST.

like image 926
André Pena Avatar asked Sep 18 '11 15:09

André Pena


1 Answers

Take a look at the following blog post which explains very nicely what happens and how the default model binder works.

Actually the default model binder uses InvariantCulture for GET parameters and culture specific format for POST parameters. Because you are sending a POST request the ISO format is not recognized. In this case you could either change the culture of your application in the <globalization> element of your web.config or write a custom model binder to override the default behavior.

like image 151
Darin Dimitrov Avatar answered Oct 13 '22 08:10

Darin Dimitrov