Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting 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:

@Html.TextBoxFor(model => model.DateOfBirth, new { style = "width:310px;height:25px" })
@Html.ValidationMessageFor(model => model.DateOfBirth)

Model Code Portion:

[Required(ErrorMessage = "Date of birth field is required")]
[DisplayName("Date of Birth:")]
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.

Please Help.

Thanks in advance
Updated 29-Oct-19 19:26pm

Solution For This:

In Web Config add this:

    <globalization culture="en-US" />

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;

Refer this link:
Share this answer
.net core
public void ConfigureServices(IServiceCollection services)
            var cultureInfo = new CultureInfo("pt-BR");
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
            CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
Share this answer
Richard Deeming 30-Oct-19 15:27pm    
The question was tagged MVC4, and .NET Core had not been released when this question was posted.

Also, changing the application culture to Brazilian Portuguese will potentially break more things than it fixes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900