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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:24:13  来源:igfitidea点击:

How to set ID and Text in html.label helper in mvc2

c#.netasp.net-mvcasp.net-mvc-2

提问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 labelelement 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 Idby a html helper method, try to use:

如果您想Id通过 html helper 方法获取,请尝试使用:

@Html.IdFor(model => model.Property)