ASP.NET MVC: putting custom attributes into option tag in select list

后端 未结 2 1619
一整个雨季
一整个雨季 2021-01-01 11:00

I\'m using ASP.NET MVC, and am trying to render a select list with the HtmlHelper.DropDownListFor method, like so:

<%= Html.DropDownListFor(x => x.AllT         


        
相关标签:
2条回答
  • 2021-01-01 11:50

    DropDownList and derivatives don't support the functionality you're looking for. But you could also simply process the string returned by DropDownList.

    <%= 
      Html.DropDownListFor(x => x.AllTopics,
        SelectListHelper.GetSelectListItems(Model.AllTopics),
        "Select a Topic", 
        new { id = "allTopics", @class = "topic-dropdown" })
      .Replace("<option", "<option attr=\"value\"")
    %>
    
    0 讨论(0)
  • 2021-01-01 11:50

    Looking at the MVC source code, DropDownListFor uses DropDownList, which uses SelectInternal, which uses ListItemToOption, which wraps calls to TagBuilder. You could modify and include this source to accept extra calls to TagBuilder when the actual option tag is built.

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