I'm using the DataType.Date attribute on my model and an EditorFor in my view. This is working fine in Internet Explorer 8 and Internet Explorer 9, but in Google Chrome it is showing a date picker and instead of displaying the value it just displays "Month/Day/Year" in faded gray text.
Why won't Google Chrome display the value?
Model:
[DataType(DataType.Date)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }
View:
<td class="fieldLabel">Est. Pur. Date</td>
<td class="field">@Html.EditorFor(m=>m.EstPurchaseDate)</td>
In MVC 3 I had to add:
among usings when adding properties:
Especially if you are adding these properties in .edmx file like me. I found that by default .edmx files don't have this using so adding only propeties is not enough.
When you decorate a model property with
[DataType(DataType.Date)]
the default template in ASP.NET MVC 4 generates an input field oftype="date"
:Browsers that support HTML5 such Google Chrome render this input field with a date picker.
In order to correctly display the date, the value must be formatted as
2012-09-28
. Quote from the specification:You could enforce this format using the
DisplayFormat
attribute:If you remove
[DataType(DataType.Date)]
from your model, the input field in Chrome is rendered astype="datetime"
and won't show the datepicker either.Reply to MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in IE
In the Model you need to have following type of declaration:
OR
You don't need to use following attribute:
At the Date.cshtml use this template:
Have fun! Regards, Blerton
In MVC5.2, add Date.cshtml to folder ~/Views/Shared/EditorTemplates:
I still had an issue with it passing the format yyyy-MM-dd, but I got around it by changing the Date.cshtml: