Getting Multiple Selected Values in Html.DropDownlistFor

前端 未结 3 1516
名媛妹妹
名媛妹妹 2020-12-13 06:50
@Html.DropDownListFor(m => m.branch, CommonMethod.getBranch(\"\",Model.branch), \"--Select--\", new { @multiple = \"multiple\" })

@Html.DropDownListFor(m => m         


        
3条回答
  •  臣服心动
    2020-12-13 07:04

    Though quite old thread but posting this answer after following other answers here, which unfortunately didn't work for me. So, for those who might have stumbled here recently or in near future, Below is what has worked for me.

    This is what helped me

    The catch for me was MultiSelectList class and I was using SelectList.

    Don't know situation in 2012 or 2015. but, now both these helper methods @Html.DropDownListFor and @Html.ListBoxFor helper methods accept IEnumerable so you can not pass any random IEnumerable object and expect these helper methods to do the job.

    These helper methods now also accept the object of SelectList and MultiSelectList classes in which you can pass the selected values directly while creating there objects.

    For example see below code how i created my multi select drop down list.

    @Html.DropDownListFor(model => @Model.arrSelectUsers, new MultiSelectList(Model.ListofUsersDTO, "Value", "Text", @Model.arrSelectUsers),
                                                        new
                                                        {
                                                            id = "_ddlUserList",
                                                            @class = "form-control multiselect-dropdown",
                                                            multiple = "true",
                                                            data_placeholder = "Select Users"
                                                        })
    

提交回复
热议问题