Html.EditorTo ignore my specified class

I have a model like:

public class MyModel { public string Name {get; set;} public DateTime StartDate {get; set;} } 

Then, in my "Razor" view, I have the following:

 @Html.EditorFor(x => x.StartDate, new { @class = "input datepicker" }) 

The resulting HTML is as follows:

 <input class="text-box single-line" data-val="true" data-val-required="The StartDate field is required." id="StartDate" name="StartDate" type="text" value="01/01/0001 00:00:00"> 

It seems like I completely ignore the definition of @class (as well as adding validation material of my own)

Is there any way to render with my specified class?

+6
source share
1 answer

You cannot set a class for the editor. You can have many different tags inside this template. Therefore, you need to assign a class inside the editor template:

 <div> @Html.TextBoxForModel(x => x.StartDate, new { @class = "input datepicker" }) </div> 

Or you can just use TextBoxFor:

 @Html.TextBoxFor(x => x.StartDate, new { @class = "input datepicker" }}) 
+9
source

All Articles