添加多个 css 类 @Html.LabelFor

Add multiple css classes @Html.LabelFor

我想添加多个 css 类 到 @Html.Label 但它没有

我已经尝试过以下方法:

   @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel" , "control-label col-md-2" })


   @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel"  "control-label col-md-2" })

这两个都给我错误

将所有 class 名称放在一个字符串中,每个名称由 space 分隔。喜欢:

@class = "SerivcesRegistrationLabel control-label col-md-2"

关闭。 Html helpers 上的所有属性,如 @id@class 都模拟了字符串将如何实际显示为属性,而无需对其进行任何实际操作。因此,您可以像在实际元素上一样指定它,在每个 class.

之后使用 space
 @Html.LabelFor(model => model.ServiceName, htmlAttributes: new { @class = "SerivcesRegistrationLabel control-label col-md-2" })

每次您在 htmlAttributes 中使用逗号时,都需要一个新属性,而不是另一个 class。