示例#1
0
        // Add Book book to shopcart and to cartJSON
        public ActionResult AddToCart()
        {
            ShoppingCart tempCart = new ShoppingCart();
            Book         book     = new Book(Request.Form["title"], Request.Form["author"], Convert.ToDecimal(Request.Form["price"]), Convert.ToInt32(Request.Form["instock"]));

            // Add book to cart
            ViewBag.AddedBookList = tempCart.AddBookToCart(book);

            if (System.Web.HttpContext.Current.Session["booksAddedToCart"] == null)
            {
                // Empty. Store item data into json-string
                System.Web.HttpContext.Current.Session["booksAddedToCart"] = tempCart.StoreCartItemsToJsonSession(tempCart.Shopcart);
            }
            else
            {
                // Not empty. Copy content to shopcart, then add to cart, then store data back into json-string
                tempCart.Shopcart = tempCart.GetCartItemsFromJsonString(System.Web.HttpContext.Current.Session["booksAddedToCart"].ToString());

                // Add book to cart
                tempCart.AddBookToCart(book);

                // Store data back into json-string to session
                System.Web.HttpContext.Current.Session["booksAddedToCart"] = tempCart.StoreCartItemsToJsonSession(tempCart.Shopcart);
            }

            // Send data to View
            ViewBag.Confirm = book.Title + " har lagts till i kundvagnen.";
            return(View("Index"));
        }