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

Posted by Superhuman on Stack Overflow See other posts from Stack Overflow or by Superhuman
Published on 2010-03-24T12:18:31Z Indexed on 2010/03/24 12:53 UTC
Read the original article Hit count: 421

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.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about object