public AdminEditViewModel AdminEditFactory(IEnumerable<ApplicationUser> users, IEnumerable<string> roles, AccountEditViewModel user,string selectedRole, AdminEditViewModel vm = null) { // Of the model does note exist, create it if (vm == null) vm = new AdminEditViewModel(); // Populate users vm.Users = users.Select(e => new SelectListItem { Value = e.Id, Text = e.FullName }); // Populate role vm.Roles = roles.Select(e => new SelectListItem { Value = e, Text = e }).OrderByDescending(item => item.Text); // reassign User vm.User = user; // Assign Selected Role vm.SelectedRole = selectedRole; // Return view model return vm; }
public ActionResult Update(AdminEditViewModel vm) { try { var srcUser = _repo.GetUserByEmployeeId(vm.User.EmployeeId); // Set the fields to update TryUpdateModel(srcUser, "User", new[] { "Email", "PhoneNumber", "FirstName", "LastName", "Title", "TitleOfCourtesy", "BirthDate", "HireDate", "Address", "City", "Region", "PostalCode", "Country", "ReportsToId" }); // Invalid model state return back to view if (!ModelState.IsValid) { return View("Edit", _factory.AdminEditFactory( _repo.GetSortedUsers(), _repo.GetRoles(), Mapper.Map<ApplicationUser, AccountEditViewModel>(srcUser), _repo.IsUserInRole(srcUser.UserName, UserRoles.User) ? UserRoles.User : UserRoles.Administrator, vm)); } _repo.EditUser(srcUser, vm.SelectedRole); Success("The User has been modified"); return RedirectToAction("Index"); } catch (UserNotFoundException) { Danger("The User you are trying to edit does not exist"); return RedirectToAction("Oops", "Home"); } }