I want to use EditorFor for a DateTime field, because then I can use data annotations to describe the format (I do not want to include the time part). I also want to assign an html class attribute (class=”date”).
When I use EditorFor helper function, I cannot assign html attributes.
When I use EditorFor and I create editor template, I can specify the class in the template, but then I lose the formatting. This is how my DateTime editor template looks like:
@ModelType DateTime
@Html.TextBox("", Model, New With {.class = "date"})
How do I make this work, so the data annotations are taken into account and so I can add the class attribute? The annotations that I talk about are:
<Display(Name:="Task start date")>
<DataType(DataType.Date)>
<DisplayFormat(DataFormatString:="{0:MM/dd/yyyy}", ApplyFormatInEditMode:=True)>
In order to have the DataAnnotations flow through, you must change your Editor Template to use
TextBoxForinstead ofTextBox, like so: