public IActionResult AddtoCart(int ToyId) { if (ModelState.IsValid) { if (db_context.CartToys.Where(o => o.ToyId == ToyId && o.CoustmerId == 1).Count() == 0) { CartToys ct = new CartToys(); ct.ToyId = ToyId; ct.CoustmerId = 1; ct.Quantity = 1; db_context.CartToys.Add(ct); db_context.SaveChanges(); } else { CartToys ct = db_context.CartToys.Where(o => o.ToyId == ToyId && o.CoustmerId == 1).FirstOrDefault(); ct.Quantity = ct.Quantity + 1; db_context.SaveChanges(); } return(RedirectToAction("Cart")); } else { return(View("Toys")); } }
public IActionResult DeleteFromCart(int CartToyId) { if (ModelState.IsValid) { if (db_context.CartToys.Where(o => o.CartToyId == CartToyId && o.CoustmerId == 1).Count() != 0) { CartToys ct = db_context.CartToys.Where(o => o.CartToyId == CartToyId && o.CoustmerId == 1).FirstOrDefault(); db_context.CartToys.Remove(ct); db_context.SaveChanges(); } return(RedirectToAction("Cart")); } else { return(View("Cart")); } }