开发者

How to map IEnumerable<SelectListItem> to IList<> for ListBox

开发者 https://www.devze.com 2022-12-24 17:14 出处:网络
I have two classes. Class1 and Class2. public class Class1{ ... public virtual IList<Class2> Class2s{get;set;}

I have two classes. Class1 and Class2.

public class Class1{
   ...
   public virtual IList<Class2> Class2s{get;set;}
   ...
}
public class Class2{
   ...
   public virtual IList<Class1> Class1s{get;set;}
   ...
}

The view contains

<%=Html.ListBox("Class2s",
                        ViewData.Model.Class2s.Select(
                                                    x => new SelectListItem {
                                                        Text = x.Name,
                                                        Value = x.Id.ToString(),
                                                        Selected =  ViewData.Model.Class1.Class2s.Any(y => y.Id == x.Id)
                                                    })

They have many to many mapping. I have a ListBox in Class1 view which displays Class2. How to map the output of the ListBox back to IList Class2s property of Class1? I am able to display the values in the ListBox but unable to map back开发者_高级运维 the SelectListItem to IList.


ToList() will do it. You'll have to import the System.Linq namespace in your aspx page.

0

精彩评论

暂无评论...
验证码 换一张
取 消