public async Task<IActionResult> AssignRole(UserAssignRoleVM model) { if (ModelState.IsValid) { bool success; if (model.User == null) { IdentityUser user = await _userManager.FindByIdAsync(model.UserId); success = await ReplaceRole(user, model.SelectedRole); } else { success = await ReplaceRole(model.User, model.SelectedRole); } if (success == false) { ViewBag.ErrorMessage = "There must always be at least one admin."; // TODO: replace ViewBag.ErrorMessage with UserIndexVM errormessages. UserIndexVM vm = new UserIndexVM(_userManager); return View(nameof(Index), vm); } } return RedirectToAction(nameof(Index)); }
public IActionResult AssignRole(string userId) { IdentityUser user = _userManager.Users.First(x => x.Id == userId); UserAssignRoleVM vm = new UserAssignRoleVM(user, _roleManager.Roles); return View(vm); }