How does a multiple select list work with model binding in ASP.NET MVC?

后端 未结 3 1247
梦毁少年i
梦毁少年i 2020-12-04 17:21

If you have a select list set to multiple in ASP.NET MVC, how does the modelbinding work?

What does it return for your selected items, an array?

<         


        
3条回答
  •  悲&欢浪女
    2020-12-04 17:59

    In VegTableViewmodel:

    public IEnumerable Vegetables { get; set; }
    

    In the Controller: Get vegetables list, and then pass it to the VegTableViewModel's Vegetables property.

    viewmodel.Vegetables = vegetables .Select(d => new MultiSelectList(d.VegName));
    

    In the View:

    @Html.ListBoxFor(m => m.L, new MultiSelectList(Model.Vegetables.Select(d => d.Items))
    

提交回复
热议问题