public async Task <IActionResult> ManageUserRoles(string userId) { ViewBag.userID = userId; var user = await userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"User with ID : {userId} cannot be found"; return(View("NotFound")); } var model = new List <ManageUserRolesModel>(); foreach (var item in roleManager.Roles) { var userrolemodel = new ManageUserRolesModel { RoleId = item.Id, RoleName = item.Name }; if (await userManager.IsInRoleAsync(user, item.Name)) { userrolemodel.IsSelected = true; } else { userrolemodel.IsSelected = false; } model.Add(userrolemodel); } return(View(model)); }
public async Task <IActionResult> Manage(string userId) { ViewBag.userId = userId; var user = await _userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"Użytkownik o ID = {userId} nie istnieje"; return(View("NotFound")); } ViewBag.UserName = user.UserName; var model = new List <ManageUserRolesModel>(); foreach (var role in _roleManager.Roles) { var userRolesViewModel = new ManageUserRolesModel { RoleId = role.Id, RoleName = role.Name }; if (await _userManager.IsInRoleAsync(user, role.Name)) { userRolesViewModel.Selected = true; } else { userRolesViewModel.Selected = false; } model.Add(userRolesViewModel); } return(View(model)); }