Search code examples
asp.net-mvcrazorhtml-helper

Html.EditorFor ignoring 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 this:

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

The outputted HTML however, looks like this:

<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 to be completely ignoring my @class definition (as well as adding validation stuff of its' own)

Is there a way of making it render with my specified class?


Solution

  • You can't set class for the the EditorFor. You could have many different tags inside this template. So you need to assign the class inside the editor template:

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

    Or you could just use the TextBoxFor:

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