// GET: ShoppingCart public ViewResult Index(ViewModels.Cart cart, string returnUrl) { return(View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl })); } ///End Index view result
}//End public RedirectRoute public RedirectToRouteResult RemoveFromCart(ViewModels.Cart cart, int productId, string returnUrl) { Product product = db.Products.FirstOrDefault(p => p.ProductId == productId); if (product != null) { cart.RemoveItem(product); } return(RedirectToAction("Index", new { controller = "Cart" })); }//End public RedirectRoute
} ///End Index view result public RedirectToRouteResult AddToCart(ViewModels.Cart cart, int productId, string returnUrl) { Product product = db.Products.FirstOrDefault(p => p.ProductId == productId); if (product != null) { cart.AddItem(product, 1); //add one quantity of this product } } return(RedirectToAction("Index", new { controller = returnUrl.Substring(1) })); }//End public RedirectRoute
public object BindModel(System.Web.Mvc.ControllerContext controllerContext, ModelBindingContext bindingContext) { // get the Cart from the session ViewModels.Cart cart = null; if (controllerContext.HttpContext.Session != null) { cart = (ViewModels.Cart)controllerContext.HttpContext.Session[sessionKey]; } // create the Cart if there wasn't one in the session data //the session helps to store and manage the Cart objects if (cart == null) { cart = new ViewModels.Cart(); if (controllerContext.HttpContext.Session != null) { controllerContext.HttpContext.Session[sessionKey] = cart; } } // return the cart return(cart); }
}//End public RedirectRoute public PartialViewResult Summary(ViewModels.Cart cart) //add a widget in the top right that summarizes the contents of the cart { return(PartialView(cart)); }