You can pass html attributes like "maxlength" or width (via "style") from the view to an editor template using EditorFor.

Here’s how you do it:

In your views, pass the attribute info to the ViewData object in the EditorFor method call with an anonymous new object initializer like this:

<%: Html.EditorFor(x => SocialSecurityNumber, new { maxlength = 9, style = "width: 100px" })%>

The html controls in your Editor Template then consume the view data object passed in the EditorFor method call.

The html controls (such as Html.TextBox) in your Editor Template look for magic strings corresponding to html standard attribute names. For example, if you want to set the "maxlength" of an input textbox, you’ll need to pass "maxlength" in the view data object. However, if you want to set the width, there is no "width" html attribute for an input box. In that case, you will need to pass "style = "width: 100px"".

In your Editor Template, such as String.ascx, be sure to pass the ViewData object to the html controls, like so:

<%: Html.TextBox("", Model, ViewData) %>

« »