public IActionResult Register(User NewUser) { if (ModelState.IsValid) { // If a User exists with provided email if (dbContext.Users.Any(u => u.Email == NewUser.Email)) { // Manually add a ModelState error to the Email field, with provided // error message ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); // You may consider returning to the View at this point } else { User newUser = new User() { Name = NewUser.Name, Email = NewUser.Email, Password = NewUser.Password, }; PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); dbContext.Add(newUser); dbContext.SaveChanges(); return(Redirect("LoginRender")); } } else { System.Console.WriteLine("This form was not valid."); return(View("Index")); } }
public IActionResult CreateEvent(Event e, string durationType) { int? userId = HttpContext.Session.GetInt32("UserId"); User planner = dbContext.Users.FirstOrDefault(u => u.UserId == userId); e.Planner = planner; if (ModelState.IsValid) { e.EventDateTime = e.EventDate.Date.Add(e.EventTime.TimeOfDay); e.Duration = e.Duration + " " + durationType; dbContext.Add(e); dbContext.SaveChanges(); return(RedirectToAction("Home")); } else { string userName = HttpContext.Session.GetString("UserName"); ViewBag.UserId = userId; ViewBag.UserName = userName; System.Console.WriteLine("NOT VALID"); return(View("NewEvent")); } }