我正在尝试将可用角色的完整列表添加到编辑用户模式中。我知道如何只列出用户的角色;但是,我想提供完整的列表,以便您可以选中/取消选中列表上的复选框,并使用"isSelected"进行更新我在这里和互联网上尝试了很多选择;但是,它们中的大多数都给我留下了类型不匹配的问题(无法将type转换为EditUserViewModel.Role(。我试图以与提取特定用户的角色相同的方式来执行此操作,但它提取了"{Microsoft.EntityFrameworkCore.InternalDbSet`1[Microsoft.AspNetCore.Identity.IdentityRole]}">作为Role并将其破坏。
方法:
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
//GET USER INFORMATION - EXIT IF USER DOESN'T EXIST
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
return View("NotFound");
}
//USER INFORMATION ---------------------------------------
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
//CompanyId = user.CompanyId
};
// ViewBag.SelectedCommpany = user.CompanyId;
//COMPANY DROPDOWN INFO------------------------------------
var company = from c in companyRepository.GetCompanys() select c;
foreach (var c in company)
{
////Store this inforamtion into the company list in the viewmodel
var companyinfo = new EditUserViewModel.CompanyList
{
CompanyName = c.CompanyName,
CompanyID = c.CompanyId
};
model.CompanyLists.Add(companyinfo);
};
//GET LIST OF ROLES(RoleID, RoleName)
var roles = roleManager.Roles;
foreach (var RoleName in roles)
{
//Execute identity method to get full information for the Role and store into an object (roleinfo)
var roleString = Convert.ToString(roles);
var fullRoleInfo = await roleManager.FindByNameAsync(roleString);
//Store this inforamtion into the Role list in the viewmodel
var roleinfo = new EditUserViewModel.Role
{
RoleName = fullRoleInfo.Name,
RoleID = fullRoleInfo.Id
};
model.Roles.Add(roleinfo);
};
}
型号:
namespace PortalDev.Models.ViewModels
{
public class EditUserViewModel
{
public EditUserViewModel()
{
Claims = new List<Claim>();
Roles = new List<Role>();
//CompanyLists = new List<ICompanyRepository>();
CompanyLists = new List<CompanyList>();
}
//ROLES ---------------------------------------------
public class Role
{
public string RoleName { get; set; }
public string RoleID { get; set; }
public bool IsSelected { get; set; }
}
public List<Role> Roles { get; set; }
//CLAIMS----------------------------------------------
public class Claim
{
public string ClaimType { get; set; }
public string ClaimID { get; set; }
}
public List<Claim> Claims { get; set; }
//COMPANY DROPDOWN--------------------------------------
public class CompanyList
{
public string CompanyName { get; set; }
public int CompanyID { get; set; }
}
[Display(Name = "Company")]
public List<CompanyList> CompanyLists { get; set; } //List of Companies for dropdown
public string SelectedCompany { get; set; }
//USER INFORMATION --------------------------------------
public string Id { get; set; }
//[Required]
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
在循环中,您试图将整个角色集转换为一个角色的名称,而不是使用单个角色名称。因此,只需更换线路:
var roleString = Convert.ToString(roles);
带有
var roleString = RoleName.Name;
使用Linq的另一种方法可能是这样的:
var allRoleViewModels = roleManager.Roles.Select(x => new EditUserViewModel.Role {RoleID = x.Id, RoleName = x.Name}).ToList();