Asp.net mvc 5 如何在ASP.NET MVC中将用户角色传递给视图模型?
管理员应该能够创建用户并选择该用户所属的角色 我的Asp.net mvc 5 如何在ASP.NET MVC中将用户角色传递给视图模型?,asp.net-mvc-5,asp.net-identity,Asp.net Mvc 5,Asp.net Identity,管理员应该能够创建用户并选择该用户所属的角色 我的CreateUserViewModel看起来像: public class CreateUserViewModel { public string Id { get; set; } public string Name { get; set; } public List<ApplicationRole> Roles { get; set; } } public ActionResult CreateUser(
CreateUserViewModel
看起来像:
public class CreateUserViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public List<ApplicationRole> Roles { get; set; }
}
public ActionResult CreateUser()
{
var model = new CreateUserViewModel();
ApplicationDbContext appDbContext = new ApplicationDbContext();
// this is the part that doesn't work because of the following error:
// Error 1
// Cannot implicitly convert type
// 'System.Data.Entity.IDbSet<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>'
// to 'System.Collections.Generic.List<AspNetMvcProject.Models.ApplicationRole>'.
// An explicit conversion exists (are you missing a cast?)
model.Roles = appDbContext.Roles;
return View();
}
公共类CreateUserViewModel
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共列表角色{get;set;}
}
我的控制器操作如下所示:
public class CreateUserViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public List<ApplicationRole> Roles { get; set; }
}
public ActionResult CreateUser()
{
var model = new CreateUserViewModel();
ApplicationDbContext appDbContext = new ApplicationDbContext();
// this is the part that doesn't work because of the following error:
// Error 1
// Cannot implicitly convert type
// 'System.Data.Entity.IDbSet<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>'
// to 'System.Collections.Generic.List<AspNetMvcProject.Models.ApplicationRole>'.
// An explicit conversion exists (are you missing a cast?)
model.Roles = appDbContext.Roles;
return View();
}
public ActionResult CreateUser()
{
var model=new CreateUserViewModel();
ApplicationDbContext appDbContext=新的ApplicationDbContext();
//这是由于以下错误而无法工作的部件:
//错误1
//无法隐式转换类型
//'System.Data.Entity.IDbSet'
//到“System.Collections.Generic.List”。
//存在显式转换(是否缺少强制转换?)
model.Roles=appDbContext.Roles;
返回视图();
}
这样做的主要目的是能够获得可用的
角色列表
,以便创建用户的管理员可以选择用户应该属于
元素中的哪些角色。您需要从实体框架数据库集中转换角色列表(Microsoft.AspNet.Identity.EntityFramework.IdentityRole
)转换为blb\u pgin\u bprp.Models.ApplicationRole
实例
一种方法是这样:
public ActionResult CreateUser()
{
var model = new CreateUserViewModel();
ApplicationDbContext appDbContext = new ApplicationDbContext();
model.Roles = appDbContext.Roles.Select(r => new lb_pgin_bprp.Models.ApplicationRole { Id = r.ID, Name = r.Name }).ToList();
return View();
}
您可以在数据库角色上使用选择,创建应用程序角色的实例,并选择ID
和名称
编辑:
您可能需要执行以下操作:
public ActionResult CreateUser()
{
var model = new CreateUserViewModel();
ApplicationDbContext appDbContext = new ApplicationDbContext();
var rolesFromDb = appDbContext.Roles.ToList();
model.Roles = rolesFromDb.Select(r => new lb_pgin_bprp.Models.ApplicationRole { Id = r.ID, Name = r.Name }).ToList();
return View();
}
谢谢!这看起来很有希望。但是这行代码:model.Roles=appDbContext.Roles.Select(r=>new AspNetMvcProject.Models.ApplicationRole{Id=r.Id,Name=r.Name})。ToList();返回“不能在LINQ to Entities查询中构造”