SelectedValues not working in MultiSelectList mvc

后端 未结 1 827
-上瘾入骨i
-上瘾入骨i 2020-12-16 00:00

I have a class like

 public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public ICollection

        
相关标签:
1条回答
  • 2020-12-16 00:08

    The last parameter of the MultiSelectList constructor takes an array of selected Id's not a collection of Category complex types.

    If you change it to this instead it will work as expected:

    product.Categories = new MultiSelectList(list, "ID", "Name", cat.CategorySelected.Select(c => c.ID).ToArray());
    

    It simply projects it into an array of Id's instead.

    See below screen shot:

    Screen grab

    Ps I also had to add this to the constructor of Category to initialize the collection:

    public Category()
    {
       CategorySelected = new List<Category>();
    }
    
    0 讨论(0)
提交回复
热议问题