コード例 #1
0
        public async Task <IActionResult> Create([Bind("ProductID,ProductName,Description,Image,Price,CategoryID")] ProductViewModel productViewModel)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(productViewModel);

                Product product = new Product
                {
                    ProductName  = productViewModel.ProductName,
                    Description  = productViewModel.Description,
                    Image        = uniqueFileName,
                    Price        = productViewModel.Price,
                    CategoryID   = productViewModel.CategoryID,
                    RestaurantID = productViewModel.RestaurantID,
                };

                _context.Add(product);
                ViewData["Category"] = new SelectList(_context.Set <Category>(), "CategoryID", "CategoryName", product.CategoryID);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(List)));
            }

            return(View());
        }
コード例 #2
0
        public CartItem AddToCart(int productId)
        {
            var userId = _userManager.GetUserId(_httpContext.User);

            Console.WriteLine($"IN ADDTOCART METHOD: {userId}");
            var cart = GetCartByUserId(userId);

            if (cart == null)
            {
                //create new cart for current user.
                cart = CreateCart();
            }

            //after adding the qty field check if the item already exists in the cart. If so increase the qty;
            //Create shopping cart item
            CartItem cartItem = null;

            if (cart.ItemsList == null)
            {
                cart.ItemsList = new List <CartItem>();
            }

            if (cart.ItemsList.Count() > 0)
            {
                cartItem = cart.ItemsList.FirstOrDefault(p => p.ProductId == productId);
            }

            if (cartItem != null)
            {
                cartItem.Quantity++;
                _dbContext.Update(cartItem);
                _dbContext.SaveChanges();
            }
            else
            {
                cartItem           = new CartItem();
                cartItem.ProductId = productId;
                cartItem.CartID    = cart.Id;
                cartItem.Quantity  = 1;
                _dbContext.Add(cartItem);
                _dbContext.SaveChanges();
            }

            return(cartItem);
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("CategoryID,CategoryName,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }