Esempio n. 1
0
        //When "Buy Now" is pressed on Index.
        public IActionResult OnGetBuyNow(int id)
        {
            //Get the product model.
            var productModel = new ProductsModel();

            Basket = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "basket");
            if (Basket == null)
            {
                //Store each Product as an Item in a new list "Basket".
                Basket = new List <Item>
                {
                    new Item
                    {
                        Product      = productModel.Find(id),
                        ItemQuantity = 1
                    }
                };
                //Store this new List in the Server Session storage.
                SessionHelper.SetObjectAsJson(HttpContext.Session, "basket", Basket);
            }
            else
            {
                //If the Basket already exists, confirm is the Item is already in the List.
                int index = Exists(Basket, id.ToString());
                if (index == -1)
                {
                    Basket.Add(new Item
                    {
                        Product      = productModel.Find(id),
                        ItemQuantity = 1
                    });
                }
                else
                {
                    //If already exist, Increment the amount of that item.
                    Basket[index].ItemQuantity++;
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "basket", Basket);
            }
            return(RedirectToPage("Basket"));
        }