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

时间秒杀一切 提交于 2019-12-01 20:12:00
Brendan Vogt

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.

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)

The accepted answer is incorrect.

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

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

Try This it works for MVC3

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

<style type="text/css">

#Percent
{

   width:100%;

}

</style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!