コード例 #1
0
ファイル: CartController.cs プロジェクト: StoyanNv/Zircon
        public async Task <IActionResult> AddToCart(int id)
        {
            var product = await cartProductService.GetProductsAsync(id);

            var products = this.HttpContext.Session.Get <List <Product> >("cart");

            if (products == null)
            {
                products = new List <Product>();
            }

            if (!products.Contains(products.FirstOrDefault(p => p.Id == id)))
            {
                products.Add(product);
            }
            else
            {
                this.TempData["__MessageType"] = MessageType.Warning;
                this.TempData["__MessageText"] = this.localizer[InfoConstants.ProductAlreadyAdded].ToString();
            }

            this.HttpContext.Session.Put("count", products.Count);
            this.HttpContext.Session.Put("cart", products);
            return(RedirectToAction("Index"));
        }