0

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; } 
3
  • dd/mm/yyyy is localized, not generic. The locale is determined by the end user's browser languages and the web app's localization settings. It's even possible to specify a locale per user by setting the request thread's CultureInfo when each request starts Commented Mar 6, 2019 at 11:43
  • the locale timezone in global file there's no such thing. The locale is a culture, eg en-US or fr-FR that specifies the language, formatting and parsing settings. Commented Mar 6, 2019 at 11:44
  • instead of the actual value from the database. what value is that? DateTime in .NET and the date types in all databases are binary values. They have no formats. Formats apply only when the dates are converted to strings for display Commented Mar 6, 2019 at 11:48

1 Answer 1

0

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; }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I tried the [DisplayFormat] but didn't enable the format in edit mode I guess... Could've saved me alot of time! Thanks alot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.