You can use the htmlAttributes parameter of @Html.Label
or @Html.LabelFor
@Html.Label("YourLabel", new { attr1 = "attr1Value", attr2 = "attr2Value" })
When using class
or other reserved words use at @
@Html.Label("YourLabel", new { @class = "classname" })
When using attributes with dashes -
, such as data attributes, use underscores _
which end up being converted to dashes -
@Html.Label("YourLabel", new { data_url = "myurl" })
However, I think support for htmlAttributes
in Html.Label
and
Html.LabelFor
was only added in MVC4, for earlier versions of MVC
you can write your own extension method.
How to extend MVC3 Label and LabelFor HTML helpers?