How to MVC 5 drop down (multiple select) box

后端 未结 2 529
感情败类
感情败类 2021-01-24 03:22

I\'m having trouble with this drop down box, just cant seem to get it right, here\'s the code:

View (Index.cshtml):

@using EvaSimulator.Models
@Model Eva         


        
2条回答
  •  生来不讨喜
    2021-01-24 03:43

    Use the For helper instead to get compile time checking. When using MultiSelectList we need to bind the selected values to an array in the first param, then you can pass your select list with the values to be shown in the second param.

     public string[] SelectedValues { get; set; }
    
     public MultiSelectList DropDownList { get; set; }
    

    Controller:

     mv.DropDownList = new MultiSelectList(/*Your list of items here*/)
    

    View:

     @Html.DropDownListFor(x => Model.SelectedValues, Model.DropDownList)
    

提交回复
热议问题