public async Task <IActionResult> Edit(int?id, Product product)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var oldProduct = await _shoppingAppContext.Products.FindAsync(id);

                    oldProduct.CategoryId  = product.CategoryId;
                    oldProduct.ProductName = product.ProductName;
                    oldProduct.ProductCost = product.ProductCost;

                    _shoppingAppContext.Update(oldProduct);
                    await _shoppingAppContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }

                return(RedirectToAction("Index"));
            }

            var categoryList = _shoppingAppContext.Categories.ToList();

            ViewData["categoryList"] = categoryList;

            return(View(product));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(int id, [Bind("OrderId,CustomerId,AmountOrdered,ProductId,OrderedAt,DisplayDate")] Order order)
        {
            if (id != order.OrderId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(order);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderExists(order.OrderId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CustomerId"] = new SelectList(_context.Customer, "CustomerId", "CustomerId", order.CustomerId);
            ViewData["ProductId"]  = new SelectList(_context.Product, "ProductId", "ProductId", order.ProductId);
            return(View(order));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ProductId,Name,ImageUrl,Description,InitialQuantity")] Product product)
        {
            if (id != product.ProductId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
        public async Task <IActionResult> Edit(int id, [Bind("CustomerId,FirstName,LastName,Email,Password,CreatedAt,DisplayDate")] Customer customer)
        {
            if (id != customer.CustomerId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Exemple #5
0
        public async Task <IActionResult> AddToCart(CartItem newCartItem)
        {
            //_logger.LogInformation($"cart item id: {cartItem.CartItemId}");
            //_logger.LogInformation($"Product ID: {cartItem.ProductId}");
            //_logger.LogInformation($"Quantity: {cartItem.Quantity}");
            //_logger.LogInformation($"Product Name: {cartItem.ProductName}");
            //_logger.LogInformation($"Product Cost: {cartItem.ProductCost}");
            //_logger.LogInformation($"Product Cost: {cartItem.ShoppingCartId}");

            var existingItem = _shoppingAppContext.CartItems.Where(cartItem => cartItem.ProductId == newCartItem.ProductId).FirstOrDefault();

            if (existingItem != null)
            {
                existingItem.Quantity += newCartItem.Quantity;
                _shoppingAppContext.Update(existingItem);
            }
            else
            {
                await _shoppingAppContext.CartItems.AddAsync(newCartItem);
            }

            await _shoppingAppContext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }