示例#1
0
        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));
        }
示例#2
0
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            // get the product id and quantity
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            // get the username
            var cartUsername = GetCartUsername();

            // check if this user has this product already in cart.  If so, update quantity
            var cartItem = _context.Cart.FirstOrDefault(c => c.ProductId == ProductId &&
                                                        c.Username == cartUsername);

            // if the product is not in the cart
            if (cartItem == null)
            {
                // add the product to the cart
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };

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


            HttpContext.Session.SetObject("Cart Item", cartItem);
            _context.SaveChanges();

            // show the cart page
            return(RedirectToAction("Cart"));
        }
示例#3
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
                {
                    // get the image
                    var files = HttpContext.Request.Form.Files;

                    if (files != null)
                    {
                        string uploadFolder = Path.Combine(_hostingEnvironment.WebRootPath, @"img");
                        // create an unique file name using guid and the file
                        string uniqueFileName = Guid.NewGuid().ToString() + files[0].FileName;
                        string filePath       = Path.Combine(uploadFolder, uniqueFileName);

                        using (var fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await files[0].CopyToAsync(fileStream);
                            product.Photo = uniqueFileName;
                        }
                    }
                    _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));
        }