public IActionResult AddUser(RolesAddUserViewModel model) { try { // make sure Administrator can not assign the role of Administrator or Superuser if (User.IsInRole("Administrator") && (model.RoleId == 1 || model.RoleId == 2)) { throw new ArgumentException("Only Superusers can assign this role."); } // New UserRole var userRole = new ApplicationUserRole() { RoleId = model.RoleId, UserId = model.UserId }; _context.UserRoles.Add(userRole); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); // i don't remember writing this } catch (ArgumentException ex) { TempData["Error"] = ex.Message; return(RedirectToAction("AddUser", new { id = model.RoleId })); } }
public IActionResult AddUser(int id) { var model = new RolesAddUserViewModel() { RoleId = id, Role = _context.Roles.FirstOrDefault(x => x.Id == id), Users = _context.Users.Select(x => new SelectListItem() { Text = x.Email, Value = x.Id.ToString() }).ToList() }; return(View(model)); }