0

I am using ASP.NET MVC 5 with ASP.NET 4.51. I am trying to validate my dates as dd/MM/yyyy eg. 31/01/1960 but model validation keeps on telling me that it is an invalid date.

Following suggestions from other SO posts:

I have the following in my web.config:

<system.web>
    <globalization uiCulture="en" culture="en-AU" />
</system.web>

My view model looks like this:

public class PassengerItem
{
    public PassengerItem()
    {
        DateOfBirth = new DateTime(1960, 1, 1);
    }

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DateOfBirth { get; set; }
}

public class BookingProcessViewModel
{
    /// <summary>
    ///     Class constructor
    /// </summary>
    public BookingProcessViewModel()
    {
        ErrorMessages = new List<string>();
        Passengers = new List<PassengerItem>();

        //Populate with 10 passengers. We show/hide these in the view. Currently we only allow a max of 4 pax to be booked at a time
        for (int i = 0; i < 4; i++)
        {
            Passengers.Add(new PassengerItem());
        }
    }

    public List<PassengerItem> Passengers { get; set; }
}

and the controller is being hit with a POST viz:

    [HttpPost]
    public ActionResult Process(BookingProcessViewModel aViewModel)
    {
    }

but I always get the following error in the model state:

enter image description here

The data entry for the date is being done using jQueryUI with the following HTML:

<input aria-invalid="false" class="form-control hasDatepicker valid" 
data-jqui-dpicker-changemonth="True" 
data-jqui-dpicker-changeyear="True" 
data-jqui-dpicker-constraininput="True" 
data-jqui-dpicker-dateformat="dd/mm/yy" 
data-jqui-dpicker-defaultdate="01/01/1901" 
data-jqui-dpicker-maxdate="24/08/2015" 
data-jqui-type="datepicker" id="Passengers_0__DateOfBirth" 
name="Passengers[0].DateOfBirth" value="31/01/1960" type="text">

So client side validation is passing, but server side there is an issue and I just cannot figure it out. This is driving me nuts. Can anyone shed any light?

Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135

1 Answers1

1

Change you web.config file to (both must be en-AU)

globalization uiCulture="en-AU" culture="en-AU" />

The DefaultModelBinder is using the value of uiCulture to convert the value to a DateTime (notice in your screenshot that its Culture: {en}

Side note. You have [DataType(DataType.Date)] which will generate the browsers HTML5 datepicker if your using @Html.EditorFor() which means that you need to use DataFormatString = "{0:yyyy-MM-dd}" in order for it to work correctly.

  • Changed the web.config to culture="en-AU" but that made no difference. I am using jQueryUI to pop up a date picker, so not EditorFor. I have included the relevant HTML that is generated for that in my original post. – TheEdge Aug 24 '15 at 05:40
  • Works fine for me. Check your screen shot again - is it now `Culture: {en-AU}`? Note the view has nothing to do with the issue - its passing back `31/1/1960` with is a valid date string for Australian culture (and since your using a jquery plugin, there is no real need for `[DataType(DataType.Date)]` whose only purpose is to render the `type="date"` attribute) –  Aug 24 '15 at 05:47
  • Hmmm. For some reason the culture is still Culture: {en}. Have double checked and the web config is definitely . Can I set the culture in code somewehre? – TheEdge Aug 24 '15 at 05:57
  • Strange. Are you using any custom model binders? Have you checked what the culture is in ISS for the web site? –  Aug 24 '15 at 06:04
  • I am not using any strange model binders. I have made this work by overriding my base controller constructor and setting the culture in there. When I force it ti en-AU in there it works. So thanks for your help. Working out why the culture is en I will track down separately. – TheEdge Aug 24 '15 at 06:11