public IActionResult DeactivateUser(int userId, UserProfile userProfile) { userId = userProfile.Id; userProfile = _UserProfileRepository.GetUserById(userId); List <UserProfile> userProfiles = _UserProfileRepository.GetAllUsers(); int totalAdmins = userProfiles.Where(up => up.UserTypeId == 2).Count(); try { if (userProfile.Activated == true) { if (userProfile.UserTypeId == 2 && totalAdmins < 2) { ModelState.AddModelError("UserTypeId", "Assign another admin before deactivating the final admin"); return(View(userProfile)); } else { _UserProfileRepository.DeactivateUser(userId); } } else { _UserProfileRepository.ReactivateUser(userId); } return(RedirectToAction("Index")); } catch { // If something goes wrong, just keep the user on the same page so they can try again return(View(userProfile)); } }
public ActionResult Deactivate(int id, ChangeUserTypeViewModel vm) { var user = _userRepo.GetById(id); try { _userRepo.DeactivateUser(user); return(RedirectToAction("Index")); } catch (Exception ex) { vm.User = user; vm.Exception = ex; return(View(vm)); } }