public RedirectToActionResult Update(UpdatedUser user) { if (ModelState.IsValid) { int? session_Id = HttpContext.Session.GetInt32("userId"); User userInSession = dbContext.Users.FirstOrDefault(u => u.UserId == session_Id); if (userInSession != null) { userInSession.FirstName = user.firstName; userInSession.LastName = user.lastName; userInSession.Email = user.email; userInSession.UpdatedAt = DateTime.Now; dbContext.SaveChanges(); return(RedirectToAction("Dashboard")); } else { int?session_Id1 = HttpContext.Session.GetInt32("userId"); int?id = session_Id1; Console.WriteLine("No user in session!!!!!!!!*******"); return(RedirectToAction("Edit")); } } else { int?session_Id = HttpContext.Session.GetInt32("userId"); int?id = session_Id; Console.WriteLine("Update failed!!!!!!!!*******"); return(RedirectToAction("Edit")); } }
public IActionResult UserUpdate(int userId, UpdatedUser user) { int? session_Id = HttpContext.Session.GetInt32("userId"); User userInSession = dbContext.Users.FirstOrDefault(u => u.UserId == session_Id); if (userInSession.IsAdmin == true) { Console.WriteLine(user.firstName); Console.WriteLine(user.lastName); Console.WriteLine(user.email); Console.WriteLine(user.IsAdmin); User selectedUser = dbContext.Users.FirstOrDefault(u => u.UserId == userId); if (ModelState.IsValid) { selectedUser.FirstName = user.firstName; selectedUser.LastName = user.lastName; selectedUser.Email = user.email; selectedUser.IsAdmin = user.IsAdmin; selectedUser.UpdatedAt = DateTime.Now; dbContext.SaveChanges(); return(RedirectToAction("AdminDashboard")); } else { int id = userId; Console.WriteLine("Update by admin failed!********"); return(RedirectToAction("AdminEdit", new { userId = id })); } } return(RedirectToAction("Logout")); }