Html 如何使用剃刀引擎在视图中为 TextAreaFor 添加 maxlength
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10586007/
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
how to add maxlength for TextAreaFor in View using razor engine
提问by Kamil
I have a TextArea() control in my View implemented using Razor Engine.
我的视图中有一个 TextArea() 控件,使用 Razor 引擎实现。
@Html.TextArea("EventNature",new { style = "width: 200px; height: 100px;" })
How can i set the Maxlength attribute for this control?
is there any built in Attribute in RazorEngine or do i have to use scripts?
如何为此控件设置 Maxlength 属性?
RazorEngine 中是否有任何内置属性,或者我必须使用脚本吗?
回答by gdoron is supporting Monica
You can do it like:
你可以这样做:
@Html.TextArea("EventNature",new { maxlength=50, // or other value
style = "width: 200px; height: 100px;" })
Just be aware it's an HTML5 attribute
请注意这是一个 HTML5 属性
maxlength HTML5
The maximum number of characters (Unicode code points) that the user can enter. If it is not specified, the user can enter an unlimited number of characters.
maxlength HTML5
用户可以输入的最大字符数(Unicode 代码点)。如果未指定,则用户可以输入无限数量的字符。
Javascript(using jQuery) validation for HTML <5:
HTML <5 的 Javascript(使用 jQuery)验证:
$('#EventNature').keypress(function(){
if (this.value.length >= 10) // allowing you to enter only 10 chars.
return false;
});?
回答by Tom Stickel
This also works for the TextAreaForhelper with more than one anonymous type in new
这也适用于具有多个匿名类型的TextAreaFor助手new
Confirmedto work finein Visual Studio 2015with MVC 5
确认以做工精细在Visual Studio中2015年与MVC 5
@Html.TextAreaFor(model => model.Comments, 5, 500, new {maxlength=4000, @class = "form-control" })
回答by kavitha Reddy
You can also restrict the length of textbox, like this:
您还可以限制文本框的长度,如下所示:
<textarea id="EventNature1" maxlength="10"></textarea>
OR
@Html.TexareaFor(m=>m.Name,new{@maxlength="10"})
回答by Jonathan
<p>Customer Number: @Html.TextBox("SearchString1",null, new {maxlength = 6})
<input type="submit" value="Filter" /></p>
The above is an example that worked for me to restrict the maximum allowed characters to be entered. The tips that show up that you can arrow through when using a .Net environment are extremely helpful. (*note, this doesn't physically change the size of the text box)
上面是一个对我有用的示例,用于限制允许输入的最大字符数。在使用 .Net 环境时可以通过箭头显示的提示非常有用。(*注意,这不会物理改变文本框的大小)
"SearchString1" = String name ----- null = Object value ------ new {maxlength = 6} = object htmlAttributes
"SearchString1" = 字符串名称 ----- null = 对象值 ------ new {maxlength = 6} = 对象 htmlAttributes
回答by Darin Dimitrov
How can i set the Maxlength attribute for this control?
如何为此控件设置 Maxlength 属性?
The maxlength
attribute is not valid for the <textarea>
element. That's not an ASP.NET MVC limitation. In the HTML specification this attribute is defined only for simple text input fields. If you want to limit the number of characters that a user could type in a textarea you could use javascript.
该maxlength
属性对<textarea>
元素无效。这不是 ASP.NET MVC 限制。在 HTML 规范中,此属性仅针对简单的文本输入字段定义。如果您想限制用户可以在 textarea 中键入的字符数,您可以使用 javascript。