Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formatting using data annotation not working

I have this model with the following data annotation because I want to handle the date in this format : dd.MM.yyyy

[DataType(DataType.Date)]
[DateRange(ErrorMessageResourceName = "DateBetween", ErrorMessageResourceType = typeof(WizardStrings))]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime? BirthDate1 { get; set; }

In the view :

@Html.EditorFor(model => model.BirthDate1 )
@Html.ValidationMessageFor(model => model.BirthDate1)

MVC still render this date with the following format : dd/MM/yyyy it seems like it doesn't take my data annotation into account.

See the rendered HTML :

<input class="text-box single-line valid" data-val="true" data-val-required="La date de naissance est requise!" id="BirthDate1" name="BirthDate1" type="text" value="**9/9/1999 12:00:00 AM**">
like image 901
Arno 2501 Avatar asked Mar 08 '26 03:03

Arno 2501


1 Answers

Before DataFormatString try adding ApplyFormatInEditMode = true

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")] 
like image 139
AndreCruz Avatar answered Mar 10 '26 15:03

AndreCruz