I see that there are some similar questions, but nobody solves my problem.
I am working on an MVC3 application with Entity Framework 4.3. I have a date field in the UK that I plan to allow the user to edit using the jQuery UI datepicker (which I received thanks to this blog ).
Luckily for me, this blog post contains instructions on creating a datepicker using the UK format, however the EF check still tells me that I need to enter the correct date format. Wierdly, this does not stop me from presenting the date in the database, it is just an unobtrusive confirmation that inserts and displays a message.
At the moment, I have the following data annotation:
[DataType(DataType.Date)] public System.DateTime Module_Date { get; set; }
but I also tried to add:
[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
which had no effect. I hope someone has a solution because I don't want to turn off unobtrusive checking in order to stop this error message.
thanks
EDIT
after @Iridio's answer, I looked into adding a binding to the model, and indeed from a few posts like this one that I read seemed to be correct, but what I came up with has no effect. here is what i 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());