//if authenticated user adds to cart, this will store the products in cart db //if the user is un authenticated, it will simply maintain cart items in session variable. public ActionResult AddToCart(int id) { try { ProductViewModel productViewModel = cartServices.GetProduct(id); List <CartViewModel> cartViewModels = new List <CartViewModel>(); if (Session["uname"] == null) { if (Session["CartItems"] != null) { cartViewModels = Session["CartItems"] as List <CartViewModel>; cartViewModels = cartServices.AddToList(cartViewModels, productViewModel); } else { cartViewModels = cartServices.AddToList(cartViewModels, productViewModel); } Session["CartCounter"] = cartViewModels.Count; Session["CartItems"] = cartViewModels; return(RedirectToAction("ProductsList", "Product")); } else { cartServices.AddToDb(Session["uname"] as string, productViewModel); return(RedirectToAction("ProductsList", "Product")); } } catch (Exception e) { return(View("Error", new HandleErrorInfo(e, "ProductsList", "Product"))); } }