¿Cómo se crea correctamente una MultiSelect <select> usando el asistente de DropdownList?
(lo siento, hay varios elementos aquí, pero ninguno parece permitirme que funcione).
Quiero crear una DropDownList que permita la selección múltiple. Puedo completar la lista, pero no puedo hacer que los valores seleccionados actualmente parezcan funcionar.
Tengo lo siguiente en mi controlador:
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.