Как правильно создать MultiSelect <select> с помощью помощника DropdownList?

(извините, здесь есть несколько пунктов, но, кажется, ни один не позволяет мне заставить это работать.)

Я хочу создать DropDownList, который позволяет множественный выбор. Я могу заполнить список, но не могу заставить выбранные значения работать.

У меня есть следующее в моем контроллере:

ViewBag.PropertyGroups = from g in db.eFinGroups
                              where g.GroupType.Contents == "P"
                              select new
                              {
                                  Key = g.Key,
                                  Value = g.Description,
                                  Selected = true
                              };<p></p>

ViewBag.SelectedPropertyGroups = from g in company.Entities .First().Properties.First().PropertyGroups select new { g.eFinGroup.Key, Value = g.eFinGroup.Description };

In the view I have:

@Html.DropDownListFor(model => model.PropertyGroupsX, 
   new MultiSelectList(ViewBag.PropertyGroups
             , "Key", "Value"
             , ViewBag.SelectedPropertyGroups), 
new { @class = "chzn-select", data_placeholder = "Choose a Property Group", multiple = "multiple", style = "width:350px;" })

PropertyGroupX is a string[] in the model.

I have tried all types of iterations with the selected properties... passing just the value, just the key, both, etc.

Also, what type is PropertyGroupX supposed to be? Is string array correct? Or should it be a dictionary that contains the current propertygroups? I really am having a hard time finding doc on this.

Someone suggested I should be using ListBoxFor. I have changed to that and still have the same issue. The selected values are not being set as selected when the option tags are rendered. Here is what I have tried:

@Html.ListBoxFor(model => model.PropertyGroups, new MultiSelectList(ViewBag.PropertyGroups, "Key", "Value"))

I have tried the model.PropertyGroups as a collection of string matching the Values, as a collection of Guid matching this IDs and as an anonymous type with both a Key and Value to match the items in the ViewBag. Nothing seems to work.

Ответы на вопрос(1)

Ваш ответ на вопрос