Question

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.

Était-ce utile?

La solution

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.

Autres conseils

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" } })

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top