我在使用多选项获取项目时遇到问题。
我有这个:
BookingObject bo = _bs.GetBookingObjects(bookingobjectID.Value).FirstOrDefault();
bo.BookingViews.Load();
MultiSelectList BookingViewsBookingObjects = new MultiSelectList(_bvs.GetBookingViews(), "BookingViewID", "BookingViewName", (IEnumerable<BookingView>)bo.BookingViews.ToList());
BookingViews&lt; - &gt; BookingViewsBookingObjects&lt; - &gt; BookingObjects
它有多对多的关系。 _bvs.GetBookingViews()返回BookingView的IQueryable,我似乎无法从bo.BookingViews中选择项目。
这里可能缺少什么?我使用的是错误的类型吗?
/ M
答案 0 :(得分:1)
如果BookingViewID为int,则可能需要在最后一个参数“BookingViewID”中发送实际所选值,而不是IEnumerable<BookingView>
作为一个洞IEnumerable<object>
,它将是IEnumerable<int>
,类似的东西:
MultiSelectList BookingViewsBookingObjects = new MultiSelectList(_bvs.GetBookingViews(), "BookingViewID", "BookingViewName", (IEnumerable<int>)bo.BookingViews.Select(a=> a.BookingViewID).ToList());
编辑:完整代码。
设置数据(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);
(查看):
<%= Html.ListBox("MultiSelectList", ViewData["Multi"] as MultiSelectList)%>
结果:
alt text http://www.diarioplus.com/files/pictures/multiselectlist.JPG