C# 如何在 mvc2 中的 html.label helper 中设置 ID 和文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14605567/
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 set ID and Text in html.label helper in mvc2
提问by LogicalDesk
I want to set ID and Text attribute in html.label helper in mvc2
我想在 mvc2 的 html.label helper 中设置 ID 和 Text 属性
<%:html.label<have to set ID and Text properties here>%>
Plz help me out..
请帮帮我..
采纳答案by Felipe Oriani
The Html.Labelmethod returns an HTML label
element and the property name of the property that is represented by the specified expression. For example:
该Html.Label方法返回的HTMLlabel
元素和由指定的表达式表示的属性的属性名称。例如:
ASPX Syntax
ASPX 语法
<%: Html.Label("Text Content", new { id = "labelId" })%>
Razor Syntax
剃刀语法
@Html.Label("Text Content", new { id = "labelId" })
The second parameter is the htmlAttributes, so, you can add any html attribute you want as a property of this anonymous object. For example:
第二个参数是 htmlAttributes,因此,您可以添加任何您想要的 html 属性作为此匿名对象的属性。例如:
new { id = "id-element", name = "name-element", size = 10, @class = "css-class" }
new { id = "id-element", name = "name-element", size = 10, @class = "css-class" }
IdFor
身份
If you want to take the Id
by a html helper method, try to use:
如果您想Id
通过 html helper 方法获取,请尝试使用:
@Html.IdFor(model => model.Property)