I have a property that I need to display on an editor
[Display(Name = "Created on")]
[UIHint("DateDisabled")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? CreationDate { get; set; }
The template editor looks like this
@model DateTime?
@{
var culture = ...// retrieve culture;
}
@Html.TextBoxFor(m => m, "{0:"+ culture.DateTimeFormat.ShortDatePattern+"}",
new { @class = "form-control", style = "width:100%", disabled = "disabled" })
And in a view I am using something like this
@Html.HiddenFor(m => m.CreationDate)
@Html.EditorFor(m => m.CreationDate)
The form contains other fields. When the editor is opened the CreationDate looks OK
<input name="CreationDate" id="CreationDate" type="hidden" value="10/12/2018 11:14:10 AM">
<input name="CreationDate" disabled="disabled" class="form-control" id="CreationDate" style="width: 100%;" type="text" value="10/12/2018">
But if the form contains some wrong value and the server posts back
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Update(Model viewModel)
{
if (!ModelState.IsValid)
{
return View("Edit", Model);
}
...
}
the creation date ignores the format and the generated html look like this
<input name="CreationDate" id="CreationDate" type="hidden" value="10/12/2018 11:14:10 AM">
<input name="CreationDate" disabled="disabled" class="form-control" id="CreationDate" style="width: 100%;" type="text" value="10/12/2018 11:14:10 AM">
As you can see the format was ignored and the full date and time is displayed.
Is this function as design? What can I do to influence the display of the date after post with errors?