4

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?

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
Martinffx
  • 2,426
  • 4
  • 33
  • 60
  • possible duplicate of [Set the class attribute to Html.EditorFor in ASP.NET MVC Razor View](http://stackoverflow.com/questions/8559028/set-the-class-attribute-to-html-editorfor-in-asp-net-mvc-razor-view) – Rap Jan 14 '14 at 19:42

4 Answers4

6

Using jQuery this could be done easily

$("input").addClass("class-name")

Input tag

@Html.EditorFor(model=>model.Name)

For DropDownlist u can use following code

$("select").addClass("class-name")

for Dropdownlist

@Html.DropDownlistFor(model=>model.Name)
Praveen M P
  • 11,314
  • 7
  • 34
  • 41
3

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.

Community
  • 1
  • 1
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
1

The accepted answer is incorrect.

Html.EditorFor ignores custom css class This behavior is by design.

http://aspnetwebstack.codeplex.com/workitem/223

wheatie
  • 31
  • 1
  • 5
0

Try This it works for MVC3

@Html.EditorFor(model => model.Percent)

<style type="text/css">

#Percent
{

   width:100%;

}

</style>
Bilal
  • 41
  • 3