I have a date field (I use jquery ui datepicker) in a form that I formatted, for example:
ViewModel
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)] public DateTime FooDate { get; set; }
View
@Html.EditorFor(m => m.FooDate)
This correctly shows the date as I want it, for example. 09-Nov-2011
The problem I get occurs when I click submit. He keeps telling me that the date is invalid .... It really is, you stupid thing!
Is there any way I can get jquery / unobtrusive javascript to ignore this field or allow this format? For now, the only way to get the form to work is if I don't format the date or use {0: d} as the date format for it.
Edit : I created a completely separate layout + view + controller + model to test this stupid thing. Still not working in IE / Safari. I have the latest jquery.validate / unobtrusive files from nuget.
My layout empty. It just downloads the following files:
"jquery-1.7.min.js" "jquery-ui-1.8.16.min.js" "jquery.validate.min.js" "jquery.validate.unobtrusive.min.js"
My TestViewModel is simple:
public class TestViewModel { [Display(Name = "Test Date:")] [DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}", ApplyFormatInEditMode = true)] public DateTime? TestDate { get; set; } }
My TestController is as follows:
public class TestController : Controller { public ActionResult Index() { var m = new TestViewModel(); m.TestDate = DateTime.Now; return View(m); } }
My view:
@using (Html.BeginForm()) { ViewContext.FormContext.ValidationSummaryId = "valSumId"; @Html.ValidationSummary(false, "The following errors were found:"); @Html.AntiForgeryToken() @Html.LabelFor(m => m.TestDate) <input type="date" id="TestDate" value="@Model.TestDate.Value.ToString("dd/MMM/yyyy")" /> <input type="submit" /> }
No work.
Do you know what this bothers? If I change TestDate to a string, it still fails.