//-------------------------------------- // // POST: /ShoppingCart/Index public ActionResult AddToCart(int? id) { try { if (ModelState.IsValid) { ShoppingCartVM scVM = new ShoppingCartVM() { }; if (id > 0) { Product product = db.Products.Find(id); if (product != null) { scVM.ShoppingCartItem.ProductId = product.ProductId; scVM.ShoppingCartItem.PricingRuleId = db.PricingRules.Find(product.PricingRuleId).PricingRuleId; } scVM.ShoppingCartItem.Quantity = 1; db.ShoppingCartItems.Add(scVM.ShoppingCartItem); db.SaveChanges(); } } return RedirectToAction("Index"); //------------------------------ } catch { return View(); } }
//-------------------------------------- // // GET: /ShoppingCart/Index public ActionResult Index() { ShoppingCartVM shoppingCartVM = new ShoppingCartVM() { }; shoppingCartVM.ProductList = db.Products.ToList(); shoppingCartVM.CartItemList = db.ShoppingCartItems.ToList(); foreach (var item in shoppingCartVM.CartItemList) { item.Product = db.Products.Find(item.ProductId); } shoppingCartVM.ShoppingCartPricingRuleList = prList; return View(shoppingCartVM); }