【发布时间】:2013-08-23 17:12:43
【问题描述】:
vs'12 asp.net C# MVC4 EF代码优先
在控制器中
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 查询中获取不属于 "Admin" 角色的用户列表,我正在尝试将它们显示在里面一个 DropDrownList
编辑:视图
@model OG.Models.UserProfiles
后来
@Html.DropDownList("DDLRoles", (SelectList)ViewData["SelectedValue"])
【问题讨论】:
-
你能贴出你认为引用
ViewBag.DDLRoles的代码吗? -
请发表您的看法。
-
那么,传递给视图的模型是 UserProfiles 类型的吗?而且,您的视图中只有 DropDownList 吗?
-
@ataravati 是的,没错,传递模型(因为我不知道如何转换它)并且视图中只有 1 个 DDL
标签: asp.net-mvc casting type-conversion