MVC Multiple DropDownLists from 1 List

前端 未结 4 1572
鱼传尺愫
鱼传尺愫 2021-01-11 23:50

Background: I have 4 dropdown lists on my page that all use one List of SelectListItem to pull data from. All 4 of these dropdowns will always

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 00:17

    In your list, need to create different SelectList entities for each of the four drop down lists like so:

    @Html.DropDownListFor(x => x.SelectedItem1, 
        new SelectList(Model.PossibleItems, "dataValue", "textValue", Model.SelectedItem1))
    @Html.DropDownListFor(x => x.SelectedItem2, Model.PossibleItems)
        new SelectList(Model.PossibleItems, "dataValue", "textValue", Model.SelectedItem2))
    @Html.DropDownListFor(x => x.SelectedItem3, Model.PossibleItems)
        new SelectList(Model.PossibleItems, "dataValue", "textValue", Model.SelectedItem3))
    @Html.DropDownListFor(x => x.SelectedItem4, Model.PossibleItems)
        new SelectList(Model.PossibleItems, "dataValue", "textValue", Model.SelectedItem4))
    

    In this example, "dataValue" and "textValue" are the properties of the SelectListItem object that correspond to your value and text elements of the drop down options.

提交回复
热议问题