asp.net-mvc mvc3 maxLength 输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10784538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mvc3 maxLength input
提问by CodeNoob
this may be kind of general but i want to limit the number of character inpit in an editorfor field.
这可能有点笼统,但我想限制 editorfor 字段中输入的字符数。
@html.EditorFor(m=>m.id,new {@maxlength="10"})
This does not seem to work.
这似乎不起作用。
回答by Andrew Backer
Try changing it to
尝试将其更改为
@Html.TextBoxFor(m=> m.id, new { maxlength="10" });
EditorFor uses the templates, which you can override yourself. I don't think they take into account the attributes you pass in like this. The TextBoxFox<> generates it in code, and should work fine.
EditorFor 使用模板,您可以自己覆盖这些模板。我不认为他们会考虑你这样传入的属性。TextBoxFox<> 在代码中生成它,应该可以正常工作。
回答by tnt
I was able to get it working in IE11 using the EditorFor and using multiple attributes.
我能够使用 EditorFor 并使用多个属性使其在 IE11 中工作。
@Html.EditorFor(model => model.CourseNumber, new { htmlAttributes = new { @style = "width:100px", @maxlength="10" } })
回答by Azarsa
In MVC3, this is better:
在MVC3中,这更好:
[StringLength(10, ErrorMessage = "Name cannot be longer than 10 characters.")]
public string Name { get; set; }
Specifies the minimum and maximum length of characters that are allowed in a data field. as in this https://stackoverflow.com/a/6802739/2127493
指定数据字段中允许的最小和最大字符长度。就像在这个https://stackoverflow.com/a/6802739/2127493

