public IActionResult Update(ViewModel FormData) { User LoggedUser = _context.users.SingleOrDefault(user => user.Userid == (int)HttpContext.Session.GetInt32("id")); ViewBag.LoggedUser = LoggedUser; if (ModelState.IsValid) { if (FormData.editUser.Username != null) { User user = _context.users.SingleOrDefault(u => u.Username == FormData.editUser.Username); if (user == null) { LoggedUser.Username = FormData.editUser.Username; _context.Update(LoggedUser); _context.SaveChanges(); TempData["success"] = "Username successfully changed!"; return(View("Account")); } if (FormData.editUser.Username == LoggedUser.Username) { } else { ModelState.AddModelError("editUser.Username", "Username already exists."); return(View("Account")); } } if (FormData.editUser.editPassword != null) { User user = _context.users.SingleOrDefault(u => u.Username == FormData.editUser.Username); PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, FormData.editUser.editPassword); _context.Update(LoggedUser); _context.SaveChanges(); TempData["success"] = "Password successfully changed!"; } if (FormData.editUser.editImage != null) { User user = _context.users.SingleOrDefault(u => u.Username == FormData.editUser.Username); user.Image = FormData.editUser.editImage; ViewBag.picture = FormData.editUser.editImage; _context.Update(LoggedUser); _context.SaveChanges(); TempData["success"] = "Password successfully changed!"; } return(View("Account")); } return(View("Account", FormData)); }
public IActionResult UserEditInfo(ViewModel FormData) { User LoggedUser = _context.Users.SingleOrDefault(user => user.Id == (int)HttpContext.Session.GetInt32("loggedid")); ViewBag.LoggedUser = LoggedUser; if (ModelState.IsValid) { if (FormData.editUser.Email != null) { User emailCheck = _context.Users.SingleOrDefault(u => u.Email == FormData.editUser.Email); if (emailCheck == null) { LoggedUser.Email = FormData.editUser.Email; _context.Update(LoggedUser); _context.SaveChanges(); } if (FormData.editUser.Email == LoggedUser.Email) { ModelState.AddModelError("editUser.Email", " "); } else { ModelState.AddModelError("editUser.Email", "Email already exists."); } } if (FormData.editUser.First_Name != null) { LoggedUser.First_Name = FormData.editUser.First_Name; _context.Update(LoggedUser); _context.SaveChanges(); } if (FormData.editUser.Last_Name != null) { LoggedUser.Last_Name = FormData.editUser.Last_Name; _context.Update(LoggedUser); _context.SaveChanges(); } return(View("Profile")); } return(View("Profile", FormData)); }
public IActionResult UpdateSite(Site FormData) { var id = HttpContext.Session.GetInt32("id"); if (id == null) { return(Redirect("/")); } Site edited_Site = _context.Sites.SingleOrDefault(site => site.Id == HttpContext.Session.GetInt32("site_id")); int site_id = edited_Site.Id; if (ModelState.IsValid) { Site siteNumberCheck = _context.Sites.SingleOrDefault(s => s.Site_Number == FormData.Site_Number); Site siteEnbCheck = _context.Sites.SingleOrDefault(s => s.Enb_Number == FormData.Enb_Number); Site siteEnb2Check = _context.Sites.SingleOrDefault(s => s.Enb2_Number == FormData.Enb2_Number); Site siteFourDigit = _context.Sites.SingleOrDefault(s => s.FourDigit_Number == FormData.FourDigit_Number); bool allUnique = true; if (siteNumberCheck != null && edited_Site.Site_Number != FormData.Site_Number) { allUnique = false; TempData["Site_Number"] = "Site number already exists"; } if (siteEnbCheck != null && edited_Site.Enb_Number != FormData.Enb_Number) { allUnique = false; TempData["Enb_Number"] = "Enb Number already exists"; } if (siteEnb2Check != null && edited_Site.Enb2_Number != FormData.Enb2_Number) { allUnique = false; TempData["Enb2_Number"] = "Enb2 Number already exists"; } if (siteEnb2Check != null && edited_Site.FourDigit_Number != FormData.FourDigit_Number) { allUnique = false; TempData["FourDigit_Number"] = "Four Digit Number already exists"; } if (allUnique == true) { edited_Site.FourDigit_Number = FormData.FourDigit_Number; edited_Site.Enb_Number = FormData.Enb_Number; edited_Site.Enb2_Number = FormData.Enb2_Number; edited_Site.Site_Name = FormData.Site_Name; edited_Site.Tech_Name = FormData.Tech_Name; edited_Site.Tech_Phone = FormData.Tech_Phone; edited_Site.Site_Number = (int)FormData.Site_Number; edited_Site.Longitude = FormData.Longitude; edited_Site.Lattitude = FormData.Lattitude; _context.Update(edited_Site); _context.SaveChanges(); return(Redirect("SiteEdited")); } return(Redirect($"EditSite/{site_id}")); } TempData["Error"] = "Please enter valid information"; return(Redirect($"EditSite/{site_id}")); }