public void Add(CartItem item) { CartItem existedItem = Items.Where(i => i.ProID == item.ProID).FirstOrDefault(); if (existedItem != null) { existedItem.Quantity += item.Quantity; } else { Items.Add(item); } }
public ActionResult AddToCart(CartItem item) { int cartItemQuantity = CurrentContext.GetCart().getQuantityPro(item.ProID); using (QLBHEntities ctx = new QLBHEntities()) { var pro = ctx.Products.Where(p => p.ProID == item.ProID).First(); if (pro.Quantity >= (item.Quantity + cartItemQuantity)) { CurrentContext.GetCart().Add(item); return RedirectToAction("Detail", "Product", new { id = item.ProID }); } else { Session["Error"] = "Loi"; return RedirectToAction("Detail", "Product", new { id = item.ProID }); } } }
public ActionResult Add1ToCart(int id) { CartItem item = new CartItem(); item.ProID = id; item.Quantity = 1; int cartItemQuantity = CurrentContext.GetCart().getQuantityPro(item.ProID); using (QLBHEntities ctx = new QLBHEntities()) { var pro = ctx.Products.Where(p => p.ProID == item.ProID).First(); if (pro.Quantity >= (item.Quantity + cartItemQuantity)) { CurrentContext.GetCart().Add(item); } else { Session["Error"] = "Loi"; } } int numberItem = CurrentContext.GetCart().GetNumberOfItem(); return Json(numberItem); }