public override void OnActionExecuting(ActionExecutingContext filterContext) { var repo = new CartRepository(Properties.Settings.Default.ConStr); if(filterContext.HttpContext.Session["cart"] != null) { var cartId = (int)filterContext.HttpContext.Session["cart"]; filterContext.Controller.ViewBag.CartCount = repo.GetCartCount(cartId); filterContext.Controller.ViewBag.CartId = cartId; } base.OnActionExecuting(filterContext); }
public ActionResult AddToCart(CartItem items, int quantity) { var cartRepo = new CartRepository(Properties.Settings.Default.ConStr); if (Session["cart"] == null) { Cart cart = cartRepo.CreateCart(); Session["cart"] = cart.CartId; } items.CartId = (int)Session["cart"]; items.Quantity = quantity; cartRepo.AddToCart(items); return Json(new { CartCount = cartRepo.GetCartCount((int)Session["cart"]), CartId = (int)Session["cart"] }, JsonRequestBehavior.AllowGet); }