My form contains a textbox for date input. When submitted the data is used to add a row to the table. The view is strongly typed to the table.
The problem is that the database server is configured with US date format. But the users need to use UK date format in the textbox. When users enter uk date format error is thrown.
The database server configuration cannot be changed. So what c开发者_开发技巧an be done so that users can enter date in uk format?
Try setting the culture in web.config:
<system.web>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB"/>
</system.web>
This is where a view model class would come in handy. The idea is that you would create another class that would make the job of the view easier. So in this case you would expose that date as a string (a correctly formatted one
) for the view. View models are pretty easy to create and maintain by using AutoMapper.
I faced the issue with my project, so after a long search about this issue the solution was to make the input type of date as text, then you have to use data annotation on your view model. This data annotation
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime Birthday { get; set; }
then it must work perfectly.
精彩评论