I'm hav开发者_StackOverflow中文版ing problems with getting items in a multiselect to get selected.
I have this:
BookingObject bo = _bs.GetBookingObjects(bookingobjectID.Value).FirstOrDefault();
bo.BookingViews.Load();
MultiSelectList BookingViewsBookingObjects = new MultiSelectList(_bvs.GetBookingViews(), "BookingViewID", "BookingViewName", (IEnumerable<BookingView>)bo.BookingViews.ToList());
BookingViews <-> BookingViewsBookingObjects <-> BookingObjects
Its a many to many relationship. _bvs.GetBookingViews() returns IQueryable of BookingView, and I cant seem to get the items from bo.BookingViews selected.
What might be missing here? am I using the wrong type?
/M
Maybe you need to send the actual selected value in the last parameter the "BookingViewID" instead of the IEnumerable<BookingView>
as a hole IEnumerable<object>
it will be an IEnumerable<int>
if BookingViewID is int, something like:
MultiSelectList BookingViewsBookingObjects = new MultiSelectList(_bvs.GetBookingViews(), "BookingViewID", "BookingViewName", (IEnumerable<int>)bo.BookingViews.Select(a=> a.BookingViewID).ToList());
EDIT: Full Code.
SetUp the Data(Controller/ViewModel):
List<KeyValuePair<int, string>> List = new List<KeyValuePair<int,string>>();;
List.Add(new KeyValuePair<int, string>(1,"Value1"));
List.Add(new KeyValuePair<int, string>(2,"Value2"));
List.Add(new KeyValuePair<int, string>(3,"Value3"));
List.Add(new KeyValuePair<int, string>(4,"Value4"));
List<int> selected = new List<int>{1,2};
ViewData["Multi"] = new MultiSelectList(List.AsEnumerable(), "Key", "Value", selected);
(View):
<%= Html.ListBox("MultiSelectList", ViewData["Multi"] as MultiSelectList)%>
The Result:
alt text http://www.diarioplus.com/files/pictures/multiselectlist.JPG
精彩评论