Extend MVC3 razor Html.LabelFor to add css class

后端 未结 2 1594
旧时难觅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" })
    

提交回复
热议问题