How can I create a SelectList with multiple selected values?

旧城冷巷雨未停 提交于 2019-12-18 18:54:21

问题


I'm trying to set multiple values in a select list:

SelectList List = new SelectList(MyListItems, "valField", "dataField", <selected values>);

What object/values do I use for to select multiple items?


回答1:


You want to use MultiSelectList instead which has a constructor to meet your needs:

public MultiSelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    IEnumerable selectedValues
)



回答2:


Example:

class Person
{
    int Id{ get; set; }
    string Name { get; set; }
}

...

var people = new List<Person>()
{
    new Person{ Id = 1, Name = "Steve" },
    new Person{ Id = 2, Name = "Bill" },
    new Person{ Id = 3, Name = "John" },
    new Person{ Id = 4, Name = "Larry" }
}
SelectList List = new MultiSelectList(people, "Id", "Name", new[]{ 2, 3 });



回答3:


Use the The Harvest Chosen jQuery plugin. See my tutorial Working with the DropDownList Box and jQuery which shows how to do this.



来源:https://stackoverflow.com/questions/7839760/how-can-i-create-a-selectlist-with-multiple-selected-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!