public ActionResult ClearCart() { var viewModel = new ProductOrderViewModel { Product = db.Products.ToList<Product>(), Category = db.Categories.ToList<Category>() }; Session["ProductOrderViewModel"] = viewModel; return View("ShoppingCart",viewModel); }
public ActionResult AddToCart(int productId) { var viewModel = (ProductOrderViewModel)Session["ProductOrderViewModel"]; if (viewModel == null) { viewModel = new ProductOrderViewModel { Product = db.Products.ToList<Product>(), Category = db.Categories.ToList<Category>() }; Session["ProductOrderViewModel"] = viewModel; } var newCart = new Cart(); bool exist = false; foreach (Cart cart in viewModel.CartItems) { if (cart.ProductId == productId) { cart.Count++; viewModel.CartTotal += cart.Product.Price; exist = true; } } if (!exist) { newCart.CartId = Guid.NewGuid().ToString(); newCart.Product = db.Products.FirstOrDefault(p => p.ProductId == productId); newCart.ProductId = productId; newCart.DateCreated = DateTime.UtcNow; newCart.Count = 1; viewModel.CartTotal += newCart.Product.Price; viewModel.CartItems.Add(newCart); } Session["ProductOrderViewModel"] = viewModel; return View("ShoppingCart",viewModel); }
public ActionResult Index() { var viewModel = (ProductOrderViewModel)Session["ProductOrderViewModel"]; if (viewModel == null) { viewModel = new ProductOrderViewModel { Product = db.Products.ToList<Product>(), Category = db.Categories.ToList<Category>() }; Session["ProductOrderViewModel"] = viewModel; } return View(viewModel); }