.NET MVC - How to assign a class to Html.LabelFor?

后端 未结 3 371
刺人心
刺人心 2021-01-31 01:08

This code

<%= Html.LabelFor(model => model.Name) %>

produces this


         


        
3条回答
  •  [愿得一人]
    2021-01-31 02:08

    Sadly, in MVC 3 the Html.LabelFor() method has no method signatures that permit a direct class declaration. However, MVC 4 adds 2 overloads that accept an htmlAttributes anonymous object.

    As with all HtmlHelpers it's important to remember that the C# compiler sees class as reserved word.

    So if you use the @ before the class attribute it works around the problem, ie:

    @Html.LabelFor(model => model.PhysicalPostcode, new { @class= "SmallInput" })
    

    The @ symbol makes the "class" a literal that is passed through.

提交回复
热议问题