public JsonResult Add(int id) { Products product = db.Products.Find(id); ProductModel model = new ProductModel { ID = product.ProductID, ProductName = product.ProductName, UnitPrice = product.UnitPrice, Quantity = 1 }; if (Session["basket"] == null) { CartModel cart = new CartModel(); cart.AddCart(model); Session["basket"] = cart; } else { CartModel cart = Session["basket"] as CartModel; cart.AddCart(model); Session["basket"] = cart; } return(Json("")); }
// Add the cart, pass the session variable info to the db public ActionResult AddCart() { CartModel model = new CartModel(_db); Tuple <int, int> retVals; string retMessage = ""; try { Dictionary <string, object> cartItems = HttpContext.Session.Get <Dictionary <string, object> >(SessionVars.Cart); retVals = model.AddCart(cartItems, HttpContext.Session.GetString(SessionVars.User)); if (retVals.Item1 > 0) // Cart Added { retMessage = "Cart " + retVals.Item1 + " Created!" + ((retVals.Item2 == 1) ? " Some goods were backordered." : ""); } else // problem { retMessage = "Cart not added, try again later"; } } catch (Exception ex) // big problem { retMessage = "Cart was not created, try again later! - " + ex.Message; } HttpContext.Session.Remove(SessionVars.Cart); // clear out current cart once persisted HttpContext.Session.SetString(SessionVars.Message, retMessage); return(Redirect("/Home")); }
// // GET: /Checkout/Cart public ActionResult Cart(string id) { CartModel cartModel = GetCartModelFromSession(); ProductModel pModel = GetProductModelFromSession(); CategoriesModel cModel = GetCategoriesModelFromSession(); cartModel.AddCart(pModel.GetProductByID(Convert.ToInt32(id)), 1); ViewData["Categories"] = cModel.GetCategories(); ViewData["Cart"] = cartModel.GetCarts(); ViewData["Notification"] = "<strong style=\"color:Red\">Product has been succesfully added to the Shopping cart</strong>"; return(View("Details")); }