我正在研究MVC2应用程序,并希望设置文本输入的maxlength属性.
我已经使用数据注释在Model对象上定义了stringlength属性,并且正确地验证了输入字符串的长度.
我不希望在模型已经拥有信息时手动设置max length属性,在我的视图中重复相同的设置.有没有办法做到这一点?
代码片段如下:
从模型:
[Required, StringLength(50)]
public string Address1 { get; set; }
Run Code Online (Sandbox Code Playgroud)
从视图:
<%= Html.LabelFor(model => model.Address1) %>
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long" })%>
<%= Html.ValidationMessageFor(model => model.Address1) %>
Run Code Online (Sandbox Code Playgroud)
我想避免做的是:
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long", maxlength="50" })%>
Run Code Online (Sandbox Code Playgroud)
我想得到这个输出:
<input type="text" name="Address1" maxlength="50" class="text long"/>
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?