public ActionResult ViewCart(ShoppingCartVM cart) { if (!ModelState.IsValid) { return(View(cart)); } string sessionID = System.Web.HttpContext.Current.Session.SessionID; ShoppingCartRepo cartRepo = new ShoppingCartRepo(); foreach (ProductVM productVM in cart.CartItems) { // update record in database Product product = productVM.CreateProductEntity(); cartRepo.AddCartItem(sessionID, product, productVM.quantity); // calculate new product totals productVM.SetTotalCost(); } // calcualte new cart totals cart.SubTotal = cart.CalculateSubTotal(); cart.Tax = cart.CalculateTax(); cart.Total = cart.CalulateTotal(); return(View(cart)); }
public ActionResult Add(ProductVM productVM) { if (!ModelState.IsValid) { return(View(productVM)); } string sessionID = System.Web.HttpContext.Current.Session.SessionID; Product product = productVM.CreateProductEntity(); int? quantity = productVM.quantity; ShoppingCartRepo cartRepo = new ShoppingCartRepo(); cartRepo.AddCartItem(sessionID, product, quantity); ViewBag.Quantity = quantity; // Session.Timeout = 1; // necessary or does i automatically do this? return(RedirectToAction("ViewCart")); }