Asp.net Core 2列出用户的角色

时间:2018-01-18 15:22:34

标签: asp.net entity-framework asp.net-core asp.net-identity

我现在正在努力.net Core 2太长了,基本上做了一个非常简单的事情:创建一个具有多个角色的用户列表。

我有一个基于已发布的示例(IdentitySampleApplication;感谢创建者)的直接启动,ApplicationUser和ApplicationRole。原始代码创建一个包含所有角色的SelectList。我想用UserRoles创建一个SelectList(用户被分配到的角色)。以下是原始代码。

public class UserController : Controller
{
    private readonly UserManager<ApplicationUser> userManager;
    private readonly RoleManager<ApplicationRole> roleManager;

    public UserController(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
    {
        this.userManager = userManager;
        this.roleManager = roleManager;
    }

    [HttpGet]
    public async Task<IActionResult> EditUser(string id)
    {
        EditUserViewModel model = new EditUserViewModel();
        model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem
        {
            Text = r.Name,
            Value = r.Id
        }).ToList();

        if (!String.IsNullOrEmpty(id))
        {
            ApplicationUser user = await userManager.FindByIdAsync(id);
            if (user != null)
            {
                model.Name = user.Name;
                model.Email = user.Email;
                model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id;
            }
        }
        return PartialView("_EditUser", model);

我想将代码更改为以下内容:

public async Task<IActionResult> EditUser(string id)
{
    EditUserViewModel model = new EditUserViewModel();

    if (!String.IsNullOrEmpty(id))
    {
        ApplicationUser user = await userManager.FindByIdAsync(id);
        if (user != null)
        {
            model.Name = user.Name;
            model.Email = user.Email;
            model.ApplicationRoles = userManager.GetRolesAsync(user);
        }
    }

EditUserViewModel是

public class EditUserViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public List<SelectListItem> ApplicationRoles { get; set; }
    [Display(Name = "Role")]
    public string ApplicationRoleId { get; set; }
}

我收到类型转换错误(cannot implicitly convert System.Threading.....IList<string> to System.Collections.....Rendering.SelectListItem),我无法解决。我显然没有走上正轨,但却想不到其他解决方案。

我非常感谢有人提供实用的解决方案。

2 个答案:

答案 0 :(得分:0)

由于您为List<string>属性分配了List<SelectListItem>,因此必须使用LINQ ex中的Select方法从List<SelectListItem>创建新的List<string>:userManager.GetRolesAsync(user) 。选择(u =&gt; new SelectListItem(){Text = u});

答案 1 :(得分:0)

感谢user3688584我创建了解决方案!以下代码创建仅包含已分配角色的SelectList

[HttpGet]
    public async Task<IActionResult> UsList(string id)
    {
        EditUserViewModel model = new EditUserViewModel();

        if (!String.IsNullOrEmpty(id))
        {
            ApplicationUser user = await userManager.FindByIdAsync(id);
            if (user != null)
            {
                var usr = await userManager.GetRolesAsync(user);
                model.Name = user.Name;
                model.Email = user.Email;
                model.ApplicationRoles = usr.Select(u => new SelectListItem() {Text = u}).ToList();
            }

        }
        return View("_UsList", model);
    }

在上一个答案中添加.ToList()就可以了。