我正在学习ASP.NET MVC。在处理下拉列表时遇到了一个小问题。
这是我在控制器中所做的:
在视野中
这会导致错误:
无法将类型为“System.Collections.Generic.List
如何解决这个问题?
处理使用查询填充下拉列表的最佳方法是什么?
这是我在控制器中所做的:
List<Int32> myList = new List<Int32>();
var a = (from m in datacontext.services
select m.ServiceID);
myList = a.ToList();
ViewData["ServiceID"] = new SelectList((IEnumerable<SelectListItem>)
a.ToList(), "ServiceID", "ServiceID");
在视野中
@Html.DropDownListFor(model => model.ServiceID, ViewData["ServiceID"] as SelectList)
这会导致错误:
无法将类型为“System.Collections.Generic.List
1[System.Int32]”的对象强制转换为类型“System.Collections.Generic.IEnumerable
1[System.Web.Mvc.SelectListItem]”。如何解决这个问题?
处理使用查询填充下拉列表的最佳方法是什么?