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

后端 未结 3 1246
梦毁少年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 18:04

    Yes, by default a multiselectlist will post through an array of the selected values.

    This article has further information, including how to use strongly-typed views with a multiselectlist.

    From the linked "article":

    • Your model or view model class needs a collection property for the IDs for the selected option items, e.g. List ToppingIds.
    • In the controller action method to which the form containing your multi-select-list POSTs, you can access the selected option items thru the collection property you added to the model or view model class.

提交回复
热议问题