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.
开发者_Go百科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.
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.
This should work for your culture: "yyyy-mm-dd HH:MM:ss"
For the record, I literally just confirmed this exact issue for en-US and it's format string should look like this DD/MM/YYYY HH:MM:SS AM|PM
which in a real example would be 09/15/2011 09:15:35 AM
Another format for a 24-hour clock would be DD/MM/YYYY HH:MM:SS
real example 09/23/2011 15:30:20
精彩评论