Once I was getting this an model generated error message while choosing date in this format 27/06/2014 (any date more then 12/mm/yy).
Error Message is: The value '27/06/2014' is not valid for Date in MVC 4
Previously my Model and View was like this:
View Code Portion:
<div class="editor-field">
@Html.TextBoxFor(model => model.DateOfBirth, new { style = "width:310px;height:25px" })
@Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
Model Code Portion:
[Required(ErrorMessage = "Date of birth field is required")]
[DisplayName("Date of Birth:")]
//[DataType(DataType.Date)]
public System.DateTime? DateOfBirth { get; set; }
Note: Here even if I had DataType portion commented for DateOfBirth model, I was getting error message. As it was not coming from model actually it was coming from controller end.
Solution For This:
In Web Config add this:
<system.web>
<globalization culture="en-US" />
</system.web>
In Global.asax.cs add this:
protected void Application_BeginRequest()
{
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
}
Error Message is: The value '27/06/2014' is not valid for Date in MVC 4
Previously my Model and View was like this:
View Code Portion:
<div class="editor-field">
@Html.TextBoxFor(model => model.DateOfBirth, new { style = "width:310px;height:25px" })
@Html.ValidationMessageFor(model => model.DateOfBirth)
</div>
Model Code Portion:
[Required(ErrorMessage = "Date of birth field is required")]
[DisplayName("Date of Birth:")]
//[DataType(DataType.Date)]
public System.DateTime? DateOfBirth { get; set; }
Note: Here even if I had DataType portion commented for DateOfBirth model, I was getting error message. As it was not coming from model actually it was coming from controller end.
Solution For This:
In Web Config add this:
<system.web>
<globalization culture="en-US" />
</system.web>
In Global.asax.cs add this:
protected void Application_BeginRequest()
{
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
}
0 comments:
Post a Comment