Reputation: 411
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.
Upvotes: 20
Views: 38947
Reputation: 139
I was able to get it working in IE11 using the EditorFor and using multiple attributes.
@Html.EditorFor(model => model.CourseNumber, new { htmlAttributes = new { @style = "width:100px", @maxlength="10" } })
Upvotes: 13
Reputation: 1308
In MVC3, this is better:
[StringLength(10, ErrorMessage = "Name cannot be longer than 10 characters.")]
public string Name { get; set; }
Specifies the minimum and maximum length of characters that are allowed in a data field. as in this https://stackoverflow.com/a/6802739/2127493
Upvotes: 4
Reputation: 8673
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.
Upvotes: 30