//function add item to cart public ActionResult AddToCart() { CartDal dal = new CartDal(); Cart cart = new Cart(); cart.Model = Request.Form["Model"].ToString(); string mo = Request.Form["Model"].ToString(); cart.Type = Request.Form["Type"].ToString(); cart.Price = Convert.ToInt32(Request.Form["Price"]); List <Cart> p = (from x in dal.cart where x.Model.Contains(mo) select x).ToList(); if (p.Count == 0) { cart.CartQuantity++; dal.cart.Add(cart); } else if (p.Count == 1) { var cartv = dal.cart.First(m => m.Model == mo); cart.CartQuantity = cartv.CartQuantity; cart.CartQuantity++; var ToDeleteC = dal.cart.First(m => m.Model == mo); dal.cart.Remove(ToDeleteC); dal.cart.Add(cart); } dal.SaveChanges(); UpdateMQuantity(cart.Model, "Add"); return(Json(cart, JsonRequestBehavior.AllowGet)); }
//removes item from cart public ActionResult RemoveFromCart() { //if (((string)Session["type"]).Equals("Employee")) CartDal dal = new CartDal(); Cart pro = new Cart(); pro.Model = Request.Form["Model"].ToString(); string mo = Request.Form["Model"].ToString(); List <Cart> p = (from x in dal.cart where x.Model.Contains(mo) select x).ToList(); if (ModelState.IsValid && p.Count != 0) { //there is such model ... delete him var ToDelete = dal.cart.First(m => m.Model == pro.Model); // Delete pro.Type = ToDelete.Type; pro.CartQuantity = ToDelete.CartQuantity; pro.Price = ToDelete.Price; dal.cart.Remove(ToDelete); dal.SaveChanges(); UpdateMQuantity(mo, "Remove"); if (pro.CartQuantity > 1) { pro.CartQuantity--; dal.cart.Add(pro); dal.SaveChanges(); } List <Cart> cartL1 = dal.cart.ToList <Cart>(); return(Json(cartL1, JsonRequestBehavior.AllowGet)); } else { return(RedirectToAction("ShoppingCart", "Buying")); } }