public IActionResult Register(User _user) { // Check initial ModelState if (ModelState.IsValid) { // If a User exists with provided email if (dbContext.Users.Any(u => u.Email == _user.Email)) { // Manually add a ModelState error to the Email field, with provided // error message ModelState.AddModelError("Email", "Email already in use!"); return(Redirect("/")); // You may consider returning to the View at this point } // Initializing a PasswordHasher object, providing our User class as its PasswordHasher <User> Hasher = new PasswordHasher <User>(); _user.Password = Hasher.HashPassword(_user, _user.Password); dbContext.Add(_user); dbContext.SaveChanges(); return(Redirect("Login")); } else { // Oh no! We need to return a ViewResponse to preserve the ModelState, and the errors it now contains! return(View("Index")); } }
public IActionResult CreateProduct(ProductsAndListProducts newPro) { if (ModelState.IsValid) { dbContext.Add(newPro.NewProduct); dbContext.SaveChanges(); return(RedirectToAction("Index")); //CHANGE LATER TO REDIRECT TO PRODUCT ID PAGE } ProductsAndListProducts viewModel = ListOfProductsViewModel(); return(View("Index", viewModel)); }
public IActionResult AddProduct(Product this_product) { Product new_product = new Product { Name = this_product.Name, Description = this_product.Description, Price = this_product.Price }; _context.products.Add(new_product); _context.SaveChanges(); return(Redirect("~/products/" + new_product.ProductId)); }
public IActionResult AddProduct(Product newProduct) { if (ModelState.IsValid) { dbContext.Products.Add(newProduct); dbContext.SaveChanges(); return(RedirectToAction("Index")); } List <Product> AllProducts = dbContext.Products.ToList(); ViewBag.AllProducts = AllProducts; return(View("Index")); }
public IActionResult CreateProduct(Product newProduct) { if (ModelState.IsValid == false) { // since we are returning the view directly here, we need to provide whatever this view needs // each Method / Action has it's OWN ViewBag, it is not shared ViewBag.products = db.Products.ToList(); return(View("Products")); } db.Products.Add(newProduct); db.SaveChanges(); return(RedirectToAction("Products")); }
public IActionResult CreateProduct(Product newProduct) { if (ModelState.IsValid) { db.Add(newProduct); int SavedProductId = db.SaveChanges(); // Console.WriteLine(newProduct.ProductId); // Console.WriteLine(SavedProductId); return(Redirect($"/product/{newProduct.ProductId}")); // return RedirectToAction("SingleProduct", new { productId = newProduct.ProductId }); } return(View("NewProduct", newProduct)); }
public IActionResult NewCategory(MasterCategory NewCategory) { if (ModelState.IsValid) { Category Category = new Category(NewCategory); dbContext.Categories.Add(Category); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { List <Category> AllCategories = CategoryList(); NewCategory.Categories = AllCategories; return(View("Index", NewCategory)); } }
public IActionResult NewProduct(MasterProduct NewProduct) { if (ModelState.IsValid) { Product Product = new Product(NewProduct); dbContext.Products.Add(Product); dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { List <Product> AllProducts = ProductList(); NewProduct.Products = AllProducts; return(View("Index", NewProduct)); } }