vs'12 asp.net C#MVC4 EF代码第一问题在控制器转换类型新selectitemlist
var usernames = Roles.GetUsersInRole("Admin");
var adminUsers = db.UserProfiles
.Where(x => !usernames.Contains(x.UserName)).ToList();
List<UserProfiles> myList = adminUsers.ToList();
IEnumerable<UserProfiles> myEnumerable = myList;
ViewBag.DDLRoles = myEnumerable;
错误进入该视图状态
The ViewData item that has the key 'DDLRoles' is of type 'System.Collections.Generic.List'1[[OG.Models.UserProfiles, OG, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' but must be of type 'IEnumerable<SelectListItem>'.
我一直在试图转换这个查询到IEnumerable<SelectListItem>
就像疯了一样,如果任何人都可以帮我转换这个正确的方式,我会很感激它。
另外,如果我没讲清楚,就是我做的是用户的列表,从LINQ查询是不是“管理”角色,我想显示他们DropDrownList
内编辑:视图
@model OG.Models.UserProfiles
再后来
@Html.DropDownList("DDLRoles", (SelectList)ViewData["SelectedValue"])
你可以发布在你的视图中引用'ViewBag.DDLRoles'的代码吗? –
请发表您的看法。 – ataravati
检查我的答案在http://stackoverflow.com/questions/17951524/mvc-and-entity-framework-select-list/17953161#17953161 –