In my ASP MVC 5 web application, I need to display a date in a specific format. When the page loads, the text is formatted correctly. But if you edit the value or simply try to post, the validation error is shown:
The field is defined like this:
@*Creation*@
<div class="form-group">
@Html.LabelFor(model => model.Creation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Creation, "{0:dd/MM/yyyy}", new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Creation)
</div>
</div>
The viewmodel property is defined as:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Creation", ResourceType = typeof(Resources.Resources))]
public DateTime Creation { get; set; }
The screenshot was created using Firefox browser. If I do the same on Edge, the validation passes, but the biding of this property on the post method is invalid (a default date value).
How can I fix this issue?
jquery.validate.js
issue (which validates dates based onMM/dd/yyyy
) and you need to override the validator (refer this answer for more detail.[DataType(DataType.Date)]
which generatestype="date"
which means the browsers implementation of a DatePicker will be displayed, but to make that work correctly you need to useDataFormatString = "{0:yyyy-MM-dd}"
(ISO format)