mvc3 maxLength input

CodeNoob picture CodeNoob · May 28, 2012 · Viewed 32k times · Source

this may be kind of general but i want to limit the number of character inpit in an editorfor field.

@html.EditorFor(m=>m.id,new {@maxlength="10"})

This does not seem to work.

Answer

Andrew Backer picture Andrew Backer · May 29, 2012

Try changing it to

@Html.TextBoxFor(m=> m.id,  new { maxlength="10" });

EditorFor uses the templates, which you can override yourself. I don't think they take into account the attributes you pass in like this. The TextBoxFox<> generates it in code, and should work fine.