MVC Multiple DropDownLists from 1 List

前端 未结 4 1576
鱼传尺愫
鱼传尺愫 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:27

    Instead of using a List, use a SelectList. Then you can reuse the list of items in DropDownListFor.

    Model:

    public SelectList PossibleItems { get; set; }
    

    Controller:

    var items = new List();
    model.PossibleItems = new SelectList(items, "Value", "Text");
    

    View:

    @Html.DropDownListFor(m => m.Item1, Model.PossibleItems)
    @Html.DropDownListFor(m => m.Item2, Model.PossibleItems)
    

提交回复
热议问题