Exemple #1
0
        public IActionResult AddtoCart(int quantity, int ProductId)
        {
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            var cartUsername = GetCartUserName();

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                _context.Cart.Add(cart);
            }
            else
            {
                cartItem.Quantity += quantity;
                _context.Update(cartItem);
            }



            _context.SaveChanges();
            return(RedirectToAction("Cart"));
        }
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //identify product Price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;
            //Determine Username
            var cartUsername = GetCartUserName();

            //Check if this user's products already exists in the cart. if so, update the quantity

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                //create and save a new cart object
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                _context.Cart.Add(cart);
            }
            else
            {
                cartItem.Quantity += Quantity;
                _context.Update(cartItem);
            }

            _context.SaveChanges();
            //show the cart page
            return(RedirectToAction("Cart"));
        }
Exemple #3
0
        public async Task <IActionResult> Edit(int id, [Bind("OrderId,OrderDate,UserId,Total,FirstName,LastName,Address,City,Province,PostalCode,Phone")] 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)));
            }
            return(View(order));
        }
Exemple #4
0
        public async Task <IActionResult> Edit(int id, [Bind("ProductId,Name,Description,Price,Photo,CategoryId")] 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)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Category, "CategoryId", "Name", product.CategoryId);
            return(View(product));
        }
        public async Task <IActionResult> Edit(int id, [Bind("CategoryId,Name")] Category category)
        {
            if (id != category.CategoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.CategoryId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Edit(int id, [Bind("OrderDetailId,OrderId,ProductId,Quantity,Price")] OrderDetail orderDetail)
        {
            if (id != orderDetail.OrderDetailId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(orderDetail);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderDetailExists(orderDetail.OrderDetailId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["OrderId"]   = new SelectList(_context.Order, "OrderId", "Address", orderDetail.OrderId);
            ViewData["ProductId"] = new SelectList(_context.Product, "ProductId", "Name", orderDetail.ProductId);
            return(View(orderDetail));
        }
Exemple #7
0
        public async Task <IActionResult> Edit(string id, [Bind("CartId,ProductId,Quantity,Price,Username")] Cart cart)
        {
            if (id != cart.CartId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(cart);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartExists(cart.CartId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductId"] = new SelectList(_context.Product, "ProductId", "Name", cart.ProductId);
            return(View(cart));
        }
        public async Task <IActionResult> Edit(string id, [Bind("Id,Name,NormalizedName,ConcurrencyStamp")] ApplicationRole applicationRole)
        {
            if (id != applicationRole.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(applicationRole);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ApplicationRoleExists(applicationRole.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(applicationRole));
        }
Exemple #9
0
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //Identify product Price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;
            // Determine Username, since it is currently hard coded

            var cartUsername = GetCartUserName();

            // Check if THIS USER's product already exists in the cart. If so, update the quantity

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                // Create and save a new Cart Object
                _context.Cart.Add(cart);
            }

            else
            {
                cartItem.Quantity += Quantity; // Add the new quantity to the existing quantity
                _context.Update(cartItem);
            }

            _context.SaveChanges();

            return(RedirectToAction("Cart"));
        }