How can I create a SelectList with multiple selected values?

前端 未结 3 898
日久生厌
日久生厌 2021-01-03 21:47

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

SelectList List = new SelectList(MyListItems, \"valField\", \"dataField\", );


        
相关标签:
3条回答
  • 2021-01-03 22:25

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

    0 讨论(0)
  • 2021-01-03 22:45

    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 });
    
    0 讨论(0)
  • 2021-01-03 22:46

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

    public MultiSelectList(
        IEnumerable items,
        string dataValueField,
        string dataTextField,
        IEnumerable selectedValues
    )
    
    0 讨论(0)
提交回复
热议问题