如何在 ASP.NET MVC 中传递用户角色以查看 ViewModel?
How to Pass User Roles to View as ViewModel in ASP.NET MVC?
管理员应该能够创建用户和select用户所属的角色。
我的 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();
}
这样做的主要目的是能够获得 list of roles
可用,以便创建用户的管理员可以 select 用户的角色应该属于 <select>
元素。
您需要将 Entity Framework DBSet (Microsoft.AspNet.Identity.EntityFramework.IdentityRole
) 中的角色列表转换为 blb_pgin_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();
}
您在数据库角色上使用 Select
创建您的 ApplicationRole 实例,选择 ID
和 Name
编辑:
您可能需要执行以下操作:
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();
}
管理员应该能够创建用户和select用户所属的角色。
我的 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();
}
这样做的主要目的是能够获得 list of roles
可用,以便创建用户的管理员可以 select 用户的角色应该属于 <select>
元素。
您需要将 Entity Framework DBSet (Microsoft.AspNet.Identity.EntityFramework.IdentityRole
) 中的角色列表转换为 blb_pgin_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();
}
您在数据库角色上使用 Select
创建您的 ApplicationRole 实例,选择 ID
和 Name
编辑:
您可能需要执行以下操作:
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();
}