html attributes for editorfor

Ghooti Farangi picture Ghooti Farangi · Mar 10, 2011 · Viewed 7.6k times · Source

How can I add html attributes such as maxlength, style, css and ... to Html.EditorFor()?

Answer

user885472 picture user885472 · Aug 9, 2011

This is way late but maybe someone else will find this helpful.

Why walk the long way? I suppose we are dealing with a string since you want to add a maxlength attribute. Then you can just use Html.TextBoxFor instead of Html.Editorfor.

TextBoxFor accepts html attributes.

@Html.TextBoxFor(model => model.Name, new{ maxlength = 50 })