2017-02-17 74 views
0

額外的屬性,我想知道是否有可能產生以下使用HTML輔助標籤標籤:標籤使用ASP.NET HTML輔助

<label class="myClass" attr1="attr1Value" attr2="attr2Value" attr3="attr3Value">labelText</label> 

它基本上有3個額外的屬性,標準的標籤標記。

回答

0

使用HtmlHelper可能將自定義屬性添加到任何元素。

@Html.Label("Content", new { attr1= "attr1" }) 

但是您需要將@添加到屬於關鍵字的屬性中,例如類

1

可以使用htmlAttributes參數的@Html.Label@Html.LabelFor

@Html.Label("YourLabel", new { attr1 = "attr1Value", attr2 = "attr2Value" }) 

當使用class或其它保留字用在@

@Html.Label("YourLabel", new { @class = "classname" }) 

當使用屬性與破折號-,如數據屬性,使用下劃線_,最終被轉換爲破折號-

@Html.Label("YourLabel", new { data_url = "myurl" }) 

不過,我覺得只在MVC4添加了對Html.LabelhtmlAttributesHtml.LabelFor支持,早期版本的MVC 你可以編寫自己的擴展方法。

How to extend MVC3 Label and LabelFor HTML helpers?