Reputation: 31
I'm making a page where profiles can be edited. Everything is working correctly except for 1 thing: On the field where you can edit the Date of birth, the value keeps defaulting to the generic "dd/mm/yyyy" letters instead of the actual value from the database.
When just showing the value in another page, there is no problem. I've tried changing the display format at the model, tried changing the locale timezone in global file, tried converting the date first to a string and then displaying it through a normal value field, giving the field class="date" and a bunch of other things. Nothing seems to be working, leaving me to wonder if I'm just missing something very stupid.
Field gets generated as follows:
<div class="form-group" id="DateOfBirth">
@Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control form-control-sm col-md-3" }})
@Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
</div>
And this gives me an input field with a date picker (and that's exactly what I want) but it displays "dd/mm/yyyy" instead of ex: 22/05/1987
Model looks like this:
[DataType(DataType.Date)]
public DateTime DateOfBirth { get; set; }
Upvotes: 0
Views: 120
Reputation: 18975
This worked for me. Please set attribute DisplayFormat
on your DateOfBirth
property.
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; };
Upvotes: 0