Search code examples
asp.netentity-frameworkasp.net-coreasp.net-identity

Asp.net Core 2 Listing users with their roles


I am now struggling .net Core 2 too long to essentially do an apparantly simple thing: Create a list of a User with its multiple roles.

I have a straightfoward Startup, ApplicationUser and ApplicationRole based on a published example (IdentitySampleApplication; thanks to the creator). The original code creates a SelectList with All roles. I would like to create a SelectList with UserRoles (Roles the user is assigned to). Below is the original code.

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);

I thought to change the code to the following:

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);
        }
    }

The EditUserViewModel is

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; }
}

I am getting type conversion errors (cannot implicitly convert System.Threading.....IList<string> to System.Collections.....Rendering.SelectListItem), which I cannot resolve. I am obviously not on the right track, but cannot think of other solutions.

I would be very thankful for someone to provide a practical solution.


Solution

  • Thanks to user3688584 I have created the solutions! The following code creates a SelectList with only the assigned roles

    [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);
        }
    

    Adding a .ToList()to the previous answer did the trick.