public ActionResult AddtoCart(Guid ProductId) { try { List<AddtoCart> products = new List<AddtoCart>(); var product = db.Products.Where(p => p.ProductID == ProductId).Single(); AddtoCart addtoCart = new Models.AddtoCart(); addtoCart.ProductId = product.ProductID; addtoCart.Discount = product.Discount; addtoCart.Price = product.Price; addtoCart.ProductDescription = product.ProductDescription; addtoCart.ProductName = product.ProductName; addtoCart.ProductImage = product.ProductImage; addtoCart.Size = product.Size; addtoCart.Qty = 1; addtoCart.MaxProduct = product.Qty; if (Session["AddtoCartItems"] != null) { var list = Session["AddtoCartItems"] as List<AddtoCart>; products = list; } //AddtoCart a = products.Find(p => p.ProductId == ProductId); //if (a != null && a.ProductName != "") //{ //} int isMatched = 0; for (int i = 0; i < products.Count; i++) { if (products[i].ProductId == ProductId) { products[i].Qty = products[i].Qty + 1; isMatched = 1; break; } } if (isMatched == 0) { products.Add(addtoCart); } Session["AddtoCartItems"] = products; return RedirectToAction("ViewCart"); } catch (Exception ex) { throw ex; } }
public ActionResult AddtoCart(Guid ProductId) { try { List<AddtoCart> products = new List<AddtoCart>(); var product = db.Products.Where(p => p.ProductID == ProductId).Single(); AddtoCart addtoCart = new Models.AddtoCart(); addtoCart.ProductId = product.ProductID; addtoCart.Discount = product.Discount; addtoCart.Price = product.Price; addtoCart.ProductDescription = product.ProductDescription; addtoCart.ProductName = product.ProductName; addtoCart.ProductImage = product.ProductImage; addtoCart.Size = product.Size; addtoCart.Qty = 1; addtoCart.MaxProduct = product.Qty; if (Session["AddtoCartItems"] != null) { var list = Session["AddtoCartItems"] as List<AddtoCart>; products = list; } //AddtoCart a = products.Find(p => p.ProductId == ProductId); //if (a != null && a.ProductName != "") //{ //} int isMatched = 0; for (int i = 0; i < products.Count; i++) { if (products[i].ProductId == ProductId) { products[i].Qty = products[i].Qty + 1; isMatched = 1; break; } } if (isMatched == 0) { products.Add(addtoCart); } Session["AddtoCartItems"] = products; return RedirectToAction("ViewCart"); } catch (Exception ex) { throw ex; } }