示例#1
0
        public IActionResult Create()
        {
            var model = new CreateDishViewModel
            {
                Dish        = new Dish(),
                Ingredients = new List <CheckBox>()
            };

            foreach (var ingredient in _ingredientService.All())
            {
                model.Ingredients.Add(new CheckBox
                {
                    Id       = ingredient.IngredientId,
                    Name     = ingredient.Name,
                    Selected = false
                });
            }

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, [Bind("CartItemId,ShoppingCartId,DishId,Quantity,Price")] CartItem cartItem, IFormCollection collection)
        {
            if (id != cartItem.CartItemId)
            {
                return(NotFound());
            }

            _cartService.RemoveCartItemIngredients(id);
            var _cartItem = _context.CartItems.Include(ci => ci.CartItemIngredients).Include(c => c.Dish).ThenInclude(ci => ci.DishIngredients).ThenInclude(m => m.Ingredient)
                            .Where(ci => ci.CartItemId == id).FirstOrDefault();

            _cartItem.Price = _cartItem.Dish.Price;

            foreach (var item in collection.Keys.Where(m => m.StartsWith("ingredient-")))
            {
                var ingStr         = item.Remove(0, 11);
                var ingId          = Int32.Parse(ingStr);
                var listIngredient = _ingredientService.All().FirstOrDefault(d => d.IngredientId == ingId);

                _cartItem.CartItemIngredients.Add(new CartItemIngredient()
                {
                    Ingredient = listIngredient
                });
                _cartItem.Price += _dishService.DishHasIngredient(_cartItem.DishId, listIngredient.IngredientId)
                    ? 0: listIngredient.Price;
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(_cartItem);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartItemExists(_cartItem.CartItemId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction("Index", "Home"));
            }
            ViewData["DishId"]         = new SelectList(_context.Dishes, "DishId", "DishId", _cartItem.DishId);
            ViewData["ShoppingCartId"] = new SelectList(_context.ShoppingCart, "ShoppingCartId", "ShoppingCartId", _cartItem.ShoppingCartId);
            return(View(_cartItem));
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("DishId,Name,Price,CategoryId")] Dish dish, IFormCollection collection)
        {
            List <Ingredient> testList = new List <Ingredient>();

            foreach (var item in collection.Keys.Where(m => m.StartsWith("ingredient-")))
            {
                var ingStr         = item.Remove(0, 11);
                var ingId          = Int32.Parse(ingStr);
                var listIngredient = _ingredientService.All().FirstOrDefault(d => d.IngredientId == ingId);

                //var listIngredient = _context.Ingredients.FirstOrDefault(d => d.IngredientId == Int32.Parse(item.Remove(0, 12)));

                testList.Add(listIngredient);

                DishIngredient di = new DishIngredient()
                {
                    Dish = dish, Ingredient = listIngredient
                };
                _context.DishIngredients.Add(di);
            }

            //foreach (var dishIngredient in _dishService.DishIngredientsForDishId(dish.DishId))
            //{
            //    dishIngredient.Enabled = collection.Keys.Any(m => m == $"ingredient-{dishIngredient.IngredientId}");
            //}

            if (ModelState.IsValid)
            {
                _context.Add(dish);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Name", dish.CategoryId);
            return(View(dish));
        }