How do I limit the length of characters in a textbox in MVC?

<%=Html.TextBox("polNum", new { maxlength = 10 }) %>

http://msdn.microsoft.com/en-us/library/dd492984.aspx

HtmlHelper uses reflection to examine the anonymous type. It converts the fields of the type into attributes on the, in this case, TextBox control. The resulting HTML looks like

<Textbox id="polNum" maxlength =10 />

You can use the anonymous type to add other relevant attributes, such as

new { @class = "MyCssClass", type = "password", value="HurrDurr", 
      textmode="multiline" }

Use the overload of the TextBox method which is getting Html attributes :

Html.TextBox( "polNum", "value", new { maxlength="10" } );

Do it in plain HTML:

<%= Html.TextBox("polNum", null, new { @maxlength = "25" }) %>

(The null parameter is because you don't want a default value...)


You need to set some html properties... something like:

<%=Html.TextBox("polNum",null, new {maxlength=10}) %>   

good luck