Adding class to EditorFor in MVC

前端 未结 1 1722
甜味超标
甜味超标 2020-12-19 13:17

I want to show Enum in EditorFor. I use Editor Template for show it.(DropDownList).

I have malty EditorFor in view. I want to set class for

相关标签:
1条回答
  • 2020-12-19 13:51

    You can pass the class name to the EditorTemplate using AdditionalViewData.

    In the main view

    @Html.EditorFor(m => m.DocumentType, new { htmlAttributes = new { @class = "myclass" } })
    

    and in the EditorTemplate

    ....
    @Html.DropDownListFor(m => m, values, ViewData["htmlAttributes"])
    

    However including the logic for the SelectList in an EditorTemplate is not good practice. I would recommend your consider creating an extension method for generating the SelectList and then this EditorTemplate wont be required. Refer this example. And Selected = v.Equals(Model), is pointless because the Selected property will be ignored (the selected item will be the value of DocumentType)

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