public IActionResult Admin_Edit_Process(UserViewModels model, int user_id) { int?Session = HttpContext.Session.GetInt32("session_id"); if (ModelState.IsValid) { User RetrievedUser = _context.Users.SingleOrDefault(u => u.UserId == user_id); RetrievedUser.email = model.AdminEditInfo.email; RetrievedUser.first_name = model.AdminEditInfo.first_name; RetrievedUser.last_name = model.AdminEditInfo.last_name; RetrievedUser.user_level = model.AdminEditInfo.user_level; _context.SaveChanges(); return(RedirectToAction("Admin_Dashboard")); } else { int id = user_id; User RetrievedUser = _context.Users.SingleOrDefault(u => u.UserId == id); ViewBag.user = RetrievedUser; return(View("AdminEdit")); } }
public IActionResult Register(UserViewModels model) { if (ModelState.IsValid) { User ReturnedValue = _context.Users.SingleOrDefault(user => user.email == model.Reg.email); if (ReturnedValue != null) { ModelState.AddModelError(string.Empty, "This email already exists!"); return(View("Index")); } else { List <User> AllUsers = _context.Users.ToList(); if (AllUsers.Count == 0) { User NewUser = new User { first_name = model.Reg.first_name, last_name = model.Reg.last_name, email = model.Reg.email, password = model.Reg.password, user_level = 9, created_at = DateTime.Now }; _context.Users.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("session_id", (int)NewUser.UserId); return(RedirectToAction("Admin_Dashboard", "Admin")); } else { User NewUser = new User { first_name = model.Reg.first_name, last_name = model.Reg.last_name, email = model.Reg.email, password = model.Reg.password, user_level = 0, created_at = DateTime.Now }; _context.Users.Add(NewUser); _context.SaveChanges(); HttpContext.Session.SetInt32("session_id", (int)NewUser.UserId); return(RedirectToAction("Dashboard")); } } } else { return(View("Index")); } }