在 Html.Textbox 上设置 required 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14276817/
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
Set required attribute on Html.Textbox
提问by LiamGu
I'm looking to create a Bootstrap styled textbox, specifically, based on the exact example below:
我正在寻找创建一个 Bootstrap 样式的文本框,具体来说,基于下面的确切示例:
<input class="span3" type="email" required>
Here's what I have so far:
这是我到目前为止所拥有的:
@Html.TextBox("CustomerEmail", null, new { @class = "input-xlarge", type = "email", required = "required" })
However, required = "required"
clearly doesn't return just required
.
但是,required = "required"
显然不只返回required
.
So, my question is, is there any way I can force it to return required like in the first example above when using Html.Textbox?
所以,我的问题是,在使用 Html.Textbox 时,有什么方法可以强制它像上面第一个示例中那样返回 required 吗?
回答by Rajpurohit
i think you should use like this
我认为你应该像这样使用
@Html.TextBoxFor(model => model.Name, new { @class = "text", type = "email", required = "required" })
i think this will help you.
我想这会帮助你。
回答by Pauline
Try
尝试
new { required = string.Empty}
As an aside, according to the W3C docs, required
is a Boolean attribute, and I quote:
顺便说required
一句,根据 W3C 文档,是一个Boolean 属性,我引用:
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
元素上布尔属性的存在表示真值,不存在该属性表示假值。如果该属性存在,则其值必须是空字符串,或者是与该属性的规范名称不区分大小写的 ASCII 匹配值,且没有前导或尾随空格。
Therefore
所以
required
required="required"
required=""
all mean the same thing.
都是一样的意思。
回答by Darin Dimitrov
You seem to have applied a different class to the textbox: input-xlarge
, whereas in your desired markup it's called span3
.
您似乎对 textbox: 应用了不同的类input-xlarge
,而在您想要的标记中,它被称为span3
.
So:
所以:
@Html.TextBox(
"CustomerEmail",
null,
new {
@class = "span3",
type = "email",
required = "required"
}
)
As far as the required part is concerned, the correct syntax here is required="required"
, otherwise you simply get broken HTML.
就所需部分而言,这里的正确语法是required="required"
,否则您只会得到损坏的 HTML。
回答by Netsurfer2
I noticed that you can also use.
我注意到你也可以使用。
required="true"
The interesting thing is that you get a warning message in Visual Studio 2015 regardless. I wonder if this is a problem with a need for updating.
有趣的是,无论如何,您都会在 Visual Studio 2015 中收到一条警告消息。我想知道这是否是需要更新的问题。
Warning Message:
警告信息:
Severity Code Description Project File Line Suppression State
Message Validation (ASP.Net): Attribute 'required' is not a valid attribute of element 'TextBox'.