public async Task <ActionResult> Add(FormCollection collection)
        {
            //get book id & quantity from the form data
            string id       = collection["Id"];
            int    quantity = Convert.ToInt32(collection["quantity"]);

            if (string.IsNullOrEmpty(id) || quantity <= 0)
            {
                return(RedirectToAction("Details", "Book", new { Id = id, Message = "failed, invalid id or quantity" }));
            }

            int itemIndex = BookIndexInCart(id);

            if (itemIndex >= 0)
            {
                UpdateItemQuantity(itemIndex, quantity);
            }
            else
            {
                cart = GetCart();
                BookDTO book = await bookstoreService.GetBookDetailsAsync(id) as BookDTO;

                cart.CartItems.Add(new CartItem {
                    Id = cart.CartItems.Count + 1, Book = book, Quantity = quantity
                });
                Session["Cart"] = cart;
            }

            //ViewBag.Message = "Added to cart";
            return(RedirectToAction("Details", "Book", new { Id = id, Message = "Book Added To Cart" }));
        }
示例#2
0
        // GET: Book/Details/5
        public async Task <ActionResult> Details(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(new HttpNotFoundResult());
            }
            BookDTO bookDTO = await bookstoreService.GetBookDetailsAsync(id) as BookDTO;

            string message = Request.QueryString["Message"];

            if (!string.IsNullOrEmpty(message))
            {
                ViewBag.Message = message;
            }
            return(View(bookDTO));
        }