I would like to limit a textbox to 10 characters in MVC.
<label ID="lbl2" runat="server" Width="20px"></label>
<%=Html.TextBox("polNum") %>
<label ID="lbl1" runat="server" Width="10px"></label>
I know you can set the Max Length property in .net. How do I do that in MVC with a textbox generated this way?
You need to set some html properties... something like:
<%=Html.TextBox("polNum",null, new {maxlength=10}) %>
good luck
Do it in plain HTML:
<%= Html.TextBox("polNum", null, new { @maxlength = "25" }) %>
(The null
parameter is because you don't want a default value...)
<%=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" }
使用获取Html属性的TextBox方法的重载 :
Html.TextBox( "polNum", "value", new { maxlength="10" } );
使用以下html设置TextBox的最大长度,在Html.TextBox中,第二个参数是textbox的值,因此,您可以传递“”或null
<%=Html.TextBox("polNum", "", new { maxlength = 10 }) %>
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control", @maxlength = "10" } })
Here i use html.Editor (do not use Textboxfor) For and i am use the Telephone number field in my database. i do not want the user to type more than ten numbers for telephone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.