I see there are some similar questions to this, but none solve my issue.
I am working on an MVC3 app with Entity Framework 4.3. I have a UK date field that i plan to allow the user to edit using the Jquery UI datepicker (which i got working thanks to this blog).
Fortunately for me this blog includes instructions on making the datepicker using UK format, however, the EF validation is still telling me that i need to enter a valid date format. Wierdly it doesn't prevent me from submitting the date to the DB its just the unobtrusive validation kicking in and displaying the message.
At the moment I have the following data annotation:
[DataType(DataType.Date)]
public System.DateTime Module_Date { get; set; }
but i have also tried adding:
[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
which had no effect at all. I hope some one has a solution because i don't fancy turning off the unobtrusive validation to stop this error message.
Thanks
EDIT
following @Iridio answer, i looked into adding a Model Binding, and indeed from the few posts like this one that i read it seemed to be the right thing to do, but what i have come up with has no effect. here is what i have tried:
public class DateTimeBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
return date;
}
}
with this in the Application_Start()
method of the Global.asax.cs
file:
ModelBinders.Binders.Add(typeof(DateTime), new DateTimeBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeBinder());