ASP MVC DateTime problem model validation problem
I have MS SQL in my local development server and production server. The DateTime format of my local server is "mm/dd/yyyy" but the format in produ开发者_StackOverflowction server is "dd/mm/yyyy". Every thing worked fine in my local server , but when I try to enter a date like "05/31/2010" in my production server, the Model Validation threw an error "The value '05/31/2010' is not valid for ...." since month cannot be greater than 12. I dont want to change the DateTime in either of the servers. Can some body tell me a way to get rid of this problem so that everything works fine in both of my server?
You could try adding the Globalization element to the web.config - Configuration > System.web:
<globalization culture="en-US" uiCulture="en-US" />
I had a similar issue - although admittedly, as a Brit, the reverse - and this fixed the problem.
I think that this article is probably what you're after, in terms of how model binding works with localization.
It's not clear where you're entering your data, but I would advise you to either explicitly tell the user to enter dates in one particular format, or use a calendar picker, or separate the three fields in the UI. Basically don't leave it ambiguous - you will get users assuming one format instead of the one you're actually using.
Note that all of this should be done well before it gets anywhere near your database - the format of your database should be irrelevant, as you should be using parameterized queries which only ever deal with DateTime
and DateTimeOffset
for time-related values.
精彩评论