0

In my controller I am returning a list that contains multiple items:

SelectedList:

  • Apple
  • Orange
  • Pear

I also have a full list that I am returning as well:

FullList:

  • Apple
  • Cranberry
  • Cherry
  • Orange
  • Grape
  • Peach
  • Plum
  • Pear

I would like to have a multi-select dropdown list that has the items in the SelectedList selected.

Currently I have this (which was setup for a single select dropdown:

var fruitDictionary = (from fl in Model.Fruits select new { Value = fl.FruitID, Text = fl.FruitName});

var fruitSelectList = new SelectList((System.Collections.IEnumerable)fruitDictionary, "Value", "Text");

@Html.ListBoxFor(fl=>fl.Fruits, fruitSelectList);

How would I select the items in the dropdown from my selectedList? I'm missing something to combine the 2 lists somehow.

webdad3
  • 8,893
  • 30
  • 121
  • 223

1 Answers1

3

Try below code:

@Html.ListBoxFor(m => m.Fruits, Model.FrSelectList)

or

@Html.DropDownListFor(m => m.Fruits, new MultiSelectList
         (Model.FrSelectList,"Value","Text"), new { multiple = "multiple" })
Lin
  • 15,078
  • 4
  • 47
  • 49