Getting Multiple Selected Values in Html.DropDownlistFor

前端 未结 3 1514
名媛妹妹
名媛妹妹 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:05

    For me it works also for @Html.DropDownListFor:

    Model:

    public class MyViewModel
    {
        public int[] SelectedValues { get; set; }
        public IEnumerable Values { get; set; }
    }
    

    Controller:

    public ActionResult Index()
    {
        var model = new MyViewModel();
    
        // the list of available values
        model.Values = new[]
        {
            new SelectListItem { Value = "2", Text = "2", Selected = true },
            new SelectListItem { Value = "3", Text = "3", Selected = true },
            new SelectListItem { Value = "6", Text = "6", Selected = true }
        };
    
        return View(model);
    }
    

    Razor:

    @Html.DropDownListFor(m => m.SelectedValues, Model.Values, new { multiple = "true" })
    

    Submitted SelectedValues in controller looks like:

提交回复
热议问题