[RESOLVED]set maxlength

HI, How can I set maxlength for @HtmlEditorFor ?


You have to specify the editor type before adding the Max Length. If it is TextBox then you can set MaxLength as:

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


The HtmlEditorFor() Helper method actually doesn’t support the htmlAttributes parameter that would allow you to pass it in manually. You can instead use a the TextBoxFor() Helper method to accomplish the same thing :

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

This is probably the easiest approach to handle this and should work just fine for most situations.

I’m using @Html.EditorFor in edit page not TextBoxfor


I’m using @Html.EditorFor in edit page not TextBoxfor

I understand that.

The EditorFor Helper does not support the use of htmlAttributes (which is how you would handle adding this attribute). Generally, the TextBoxFor helper is going to be equivalent to using the EditorFor in most use-cases. Have you tried using the
TextBoxFor approach to see if that works for you?

If it doesn’t, we can look at some alternative solutions, but they are generally not going to be as clean as the suggested method.

Leave a Reply