Search code examples
asp.net-mvcasp.net-mvc-4html-helperrazor-2

How to add custom data attributes and classes to `@Html.EditorFor`?


I want to add some custom attributes to the input generated by @Html.EditorFor, I tried the following:

@Html.EditorFor(model => model.Percent, new { @class = "percent" })

But it just ignores my class, from what I can tell from searching around is that the template doesn't support adding custom attributes.

But how does one create a custom template adding support for the custom attributes, while keeping all the functionality of the old template?


Solution

  • Please see the following posts, this question has been asked before on Stackoverflow.

    There are many more examples, just Google it.

    I hope this helps.