views:

212

answers:

1

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.

A: 

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

Will
Html.ListBox will accept IEnumerable<SelectedListItem>. So where do I need to call ToList()?
Superhuman