Extend MVC3 razor Html.LabelFor to add css class

后端 未结 2 1604
旧时难觅i
旧时难觅i 2021-02-13 10:47

I am trying to add a css class to Html.LabelFor on an EditorTemplate

 @Html.LabelFor(model => model.Name, new { @class = \"myLabel\" })

my

相关标签:
2条回答
  • 2021-02-13 11:21

    I suspect that you forgot to bring the namespace in which you defined this LabelExtensions class in scope in the view. So for example if this class is defined inside the MyProject.Extensions namespace:

    namespace MyProject.Extensions
    {
        public static class LabelExtensions
        {
            ...
        }
    }
    

    make sure that you've brought this namespace into scope:

    @using MyProject.Extensions
    @model MyViewModel
    ...
    @Html.LabelFor(model => model.Name, new { @class = "myLabel" })
    
    0 讨论(0)
  • 2021-02-13 11:26

    You're using html.LabelHelper

    However you have defined your own LabelHelper method, so use it =)

    0 讨论(0)
提交回复
热议问题