public async Task<ActionResult> Edit(string id) { var role = await RoleManager.FindByIdAsync(id); if (role == null) return HttpNotFound(); var viewModel = new EditRoleViewModel { Id = role.Id, Name = role.Name, RoleAccesses = new List<RoleAccess>(role.RoleAccesses), Controllers = GetControllers() }; return View(viewModel); }
public async Task<ActionResult> Edit(EditRoleViewModel viewModel) { if (!ModelState.IsValid) { viewModel.Controllers = GetControllers(); return View(viewModel); } var role = await RoleManager.FindByIdAsync(viewModel.Id); role.Name = viewModel.Name; var dbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>(); dbContext.RoleAccesses.RemoveRange(role.RoleAccesses); foreach (var controller in viewModel.SelectedControllers) { foreach (var action in controller.Actions) { role.RoleAccesses.Add(new RoleAccess { Action = action.Name, Controller = controller.Name, RoleId = role.Id, Role = role, }); } } await RoleManager.UpdateAsync(role); await dbContext.SaveChangesAsync(); return RedirectToAction("Index"); }