public IActionResult Edit(UsersEditViewModel viewModel) { viewModel.CanDelete = viewModel.Id != ApplicationUser.Id; if (!ModelState.IsValid) { return(View(viewModel)); } User user = Models.User.GetById(Database.Connection, viewModel.Id); if (user == null || user.IsDeleted) { return(NotFound()); } if (Models.User.IsUserNameInUse(Database.Connection, viewModel.UserName, viewModel.Id)) { ModelState.AddModelError(nameof(UsersEditViewModel.UserName), "Username is already in use."); return(View(viewModel)); } if (!string.IsNullOrWhiteSpace(viewModel.NewPassword)) { user.Password = authenticationBusinessLogic.HashPassword(viewModel.NewPassword); } user.UserName = viewModel.UserName; user.Save(Database.Connection); TempData["Message"] = "User saved successfully."; return(RedirectToAction(nameof(Edit), new { id = user.Id })); }
public IActionResult Edit(int id) { User user = Models.User.GetById(Database.Connection, id); if (user == null || user.IsDeleted) { return(NotFound()); } UsersEditViewModel viewModel = new UsersEditViewModel { Message = (string)TempData["Message"], CanDelete = user.Id != ApplicationUser.Id, Id = user.Id, UserName = user.UserName }; return(View(viewModel)); }