Click here to Skip to main content
15,886,258 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:")]
//[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.

Please Help.

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

XML
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;
        }

Refer this link: 

http://coderskey.blogspot.in/2014/06/the-value-27062014-is-not-valid-for.html
 
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
 
Comments
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