I have this application, and the default date format must be dd/MM/yyyy
(the language is Brazilian Portuguese).
I already had set culture and UI culture to pt-BR
and now myDate.ToShortDateString()
returns the dates as I want. I have no trouble displaying them.
The problem is, when the user fills an input field with a date such as 17/08/2011
and submits the form the DateTime parameter to my action becomes null. If I supply a date in the format 08/17/2011
, it works fine.
How can I make the ASP.Net MVC model binding to parse my dates correctly?
I found what happended. My form was posting via
GET
method, and the MVC just uses the culture for an action parameter when it's passed in theRouteData
or by the form viaPOST
method.I just changed the form to
POST
method and it worked.I'm pretty sure the issue is a lack of a DateTimeFormat on your DateTime types.
EDIT**
Another thing you want to be sure of is that the "name" property of your input element matches what you're passing into your action. If it doesn't null will appear on POST action every time.
"dateParam" Should match the name property here.
Try this on your property in the view model: