Addind css classes to razor elements

后端 未结 3 832
无人共我
无人共我 2021-01-12 03:03

In razor if I had something like:

@Html.EditorFor(model => model.name) or even: @Html.CheckBoxFor(m => m.RememberMe)

How woul

3条回答
  •  无人及你
    2021-01-12 03:38

    You cannot do that with the EditorFor helper simply because you don't know what template will be used. You could achieve it but you will need to write a custom editor template. For example this could be achieved by overriding the default editor template and taking into account the second parameter which represents an additional view data.

    Here's an example of how such a custom editor template could look for string types (~/Views/Shared/EditorTemplates/string.cshtml):

    @Html.TextBox(
        "", 
        ViewData.TemplateInfo.FormattedModelValue,
        ViewData
    )
    

    and then you could use it like that:

    @Html.EditorFor(model => model.name, new { @class = "myclass" })
    

    With the CheckBoxFor helper you could do that:

    @Html.CheckBoxFor(m => m.RememberMe, new { @class = "myclass" })
    

提交回复
热议问题