jQuery time picker and MVC validation - the field must be a date

TheVillageIdiot picture TheVillageIdiot · Oct 25, 2012 · Viewed 7.8k times · Source

I have a property named StartDate in my model. I have used timepicker add-on to jquery.ui plugin from here. It is working fine, but problem is that I'm getting validation error message. How can I change validation so that it passes for date-time.

The model is:

public class DateTimePickerTest : EntityBase
{
    [DataType(DataType.DateTime)]
    [DisplayName("Start Date")]
    public DateTime StartDate { get; set; }
    [DataType(DataType.DateTime)]
    [DisplayName("End Date")]
    public DateTime EndDate { get; set; }
}

Following is the error message

I'm mostly following directions given here on MSDN by Rick Anderson.

Answer

TheVillageIdiot picture TheVillageIdiot · Oct 26, 2012

Saved by this answer.

I have used DisplayFormat attribute with ApplyFormatInEditMode property set to true:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.mm.yy}")]