I have the following datetime field
[DisplayName("Expiry Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime ExpiryDate { get; set; }
And then I display it like so (top line is me checking I actually have a value for ExpiryDate - it displays as I would expect)
<div>
Current value: @Model.ExpiryDate.ToShortDateString()
@Html.TextBoxFor(model => model.ExpiryDate, "{0:dd/MM/yyyy}" , new { type = "date", @class = "form-control" })
</div>
The date picker (in Chrome) works correctly for selecting a date - I can select a date and it correctly binds back to my model on submit. It also works fine when ExpiryDate doesn't have a value, displaying "dd/mm/yyyy"
But when ExpiryDate does have a value, the textbox still displays dd/mm/yyyy rather than 22/02/2019 or whatever the value is
Strangely, the textbox appears to have the correct value when I inspect the generated HTML, as below
<input class="form-control" data-val="true" data-val-date="The field Expiry Date must be a date." data-val-required="The Expiry Date field is required." id="ExpiryDate" name="ExpiryDate" type="date" value="22/02/2019">
I've also tried explicitly including the value, including @value = Model.ExpiryDate.ToShortDateString()
in the HtmlAttributes, which makes no difference (not that I particularly expected it to, since the value is already correct)
What am I missing?