public ActionResult EditRole(int id) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); RolesRepository rolesRepository = new RolesRepository(context); AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context); RolesEditRoleVM model = new RolesEditRoleVM(); if (id > 0) { Role role = rolesRepository.GetAll(filter: r => r.ID == id, includeProperties: "AuthenticatingActions").FirstOrDefault(); PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository); model.ID = role.ID; model.Name = role.Name; } else { Role role = new Role(); role.AuthenticatingActions = new List <AuthenticatingAction>(); PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository); } return(View(model)); }
public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); Role role = null; if (!this.ModelState.IsValid) { AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context); var authenticatingActions = authenticatingActionsRepository.GetAll(); List <AuthenticatingActionsVM> authenticatingActionsViewModel = new List <AuthenticatingActionsVM>(); foreach (var action in authenticatingActions) { authenticatingActionsViewModel.Add(new AuthenticatingActionsVM { ID = action.ID, Name = action.Name, IsAssigned = false }); } ViewBag.AuthenticatingActions = authenticatingActionsViewModel; return(View(model)); } using (UnitOfWork unitOfWork = new UnitOfWork(context)) { try { var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork); var rolesRepository = new RolesRepository(unitOfWork); if (model.ID > 0) { role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault(); } else { role = new Role(); role.AuthenticatingActions = new List <AuthenticatingAction>(); } role.Name = model.Name; UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository); rolesRepository.Save(role); PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.RollBack(); throw ex; } } return(RedirectToAction("Index", "Roles")); }