MVC 2 EditorFor MaxLength和Size

ara*_*333 7 asp.net-mvc

如何在MVC 2中设置TextBox的MaxLength和Size?关于SO的问题已经提出了几个问题.作为MVC的新手,我发现没有一个解决方案似乎有效 - 可能是因为我做得不对.

无论如何,我想知道是否有更好的解决方案,因为之前的问题似乎是在MVC 2处于测试阶段时提出的.

Łuk*_*.pl 9

这很容易,你只需要额外的htmlAtttributes

<%= Html.TextBoxFor(model => model.Member, new { maxlength = "20", width = "15" }) %> 
<%= Html.TextBoxFor(model => model.Member, new { maxlength = "20", style = "width:200px;" }) %> 
Run Code Online (Sandbox Code Playgroud)

  • 主题标题说EditorFor - 回答说TextBoxFor.仅供参考 - 额外的HtmlAttributes不能与EditorFor一起使用,除非您使用System.ComponentModel.DataAnnotations"[StringLength(5,ErrorMessage ="必须是5个或更少的字符")]"来装饰属性,否则需要更改为TextBoxFor然后我们可以使用答案中提到的额外HtmlAttributes. (7认同)