コード例 #1
0
        public IActionResult AddToCart(int id, int quantity)
        {
            var    product   = ECommData.GetProduct(id);
            var    totalCost = quantity * product.UnitPrice;
            string message   = $"You added {product.ProductName} " +
                               $"(x{quantity}) to your cart at a total cost of {totalCost:C}.";

            var cart     = ShoppingCart.GetFromSession(HttpContext.Session);
            var lineItem = cart.LineItems.SingleOrDefault(item => item.Product.Id == id);

            if (lineItem != null)
            {
                lineItem.Quantity += quantity;
            }
            else
            {
                cart.LineItems.Add(new ShoppingCart.LineItem
                {
                    Product = product, Quantity = quantity
                });
            }

            ShoppingCart.StoreInSession(cart, HttpContext.Session);
            Logger.LogInformation("*** AddToCart({id}, {quantity}): Cart Updated", id, quantity);

            return(PartialView("_AddedToCart", message));
        }
コード例 #2
0
        public IActionResult Detail(int id)
        {
            //ViewBag.guid1 = ECommData.ToString();
            //ViewBag.guid2 = ECommData.ToString();

            var product = ECommData.GetProduct(id);

            if (product == null)
            {
                Logger.LogError("!!! Detail({id}): Not Found", id);
                return(NotFound());
            }
            Logger.LogInformation("*** Detail({id}): Found", id);
            return(View(product));
        }