Html.ListBox

I am trying to iterate over items in a list using asp.net mvc

Html.ListBox("SupplierId",
             new SelectList(Model.Suppliers, "Id", "Name", Model.SelectedSuppliers))

      

Below is a view

var viewData = new ViewData.SubstrateEditViewData(
               new DataAccess.SubstrateRepository().GetItemById(id),
               new DataAccess.SupplierRepository().GetItems(),
               new DataAccess.SupplierSubstrateRepository().GetItems().Where(s =>  s.SubstrateId ==id).Select(s => s.Supplier));

      

for some reason its no items selected, even though I can see the Model.SelectedSupplier containing two provider objects.

thanks

+1


a source to share


3 answers


Note that only item ids need to be passed to the selectedValues ​​parameter of the MultiSelectList () method, so you must use



Html.ListBox("SupplierId", new MultiSelectList(Model.Suppliers, "Id", "Name",
    Model.SelectedSuppliers.Select(s => s.Id)))

      

+6


a source


the documentation for the SelectList constructor refers to a single value. It's not like walking through a List or IEnumerable values ​​will result in a list with multiple selected values.



0


a source


I was struggling with the same problem a few weeks ago. The default extension methods for MultiSelect lists do not behave as expected. I ended up just scrolling through the elements myself and setting their property selected

manually.

0


a source







All Articles