コード例 #1
0
        public async Task <IActionResult> UnSetIngredients(int?recipeId)
        {
            IQueryable <RecipeIngredient> recipeIngredients = _context.RecipeIngredient;
            //List<RecipeIngredient> recipeIngredients = await _context.RecipeIngredient.ToListAsync();
            List <Recipe> recipes = await _context.Recipe.ToListAsync();

            recipeIngredients = recipeIngredients.Include(e => e.Recipe).ThenInclude(r => r.Ingredients);
            if (recipeId != null)
            {
                recipeIngredients = recipeIngredients.Where(r => r.RecipeId == recipeId);
            }
            if (recipeId != null)
            {
                recipeIngredients = recipeIngredients.Include(r => r.Ingredient);
            }
            else
            {
                recipeIngredients = null;
            }

            var unSetIngredients = new UnSetIngredientsViewModel
            {
                Recipes             = new SelectList(recipes, "Id", "Title"),
                RecipeIngredients   = (recipeIngredients != null ? new SelectList(await recipeIngredients.OrderBy(r => r.Ingredient.Name).ToListAsync(), "Id", "Ingredient.Name") : null),
                RecipeIngredientIds = (recipeIngredients != null ? await recipeIngredients.Select(r => r.Id).ToListAsync() : null)
            };

            return(View(unSetIngredients));
        }
コード例 #2
0
        public async Task <IActionResult> UnSetIngredients(UnSetIngredientsViewModel entry)
        {
            if (ModelState.IsValid)
            {
                foreach (int riId in entry.RecipeIngredientIds)
                {
                    RecipeIngredient recipeIngredient = await _context.RecipeIngredient
                                                        .FirstOrDefaultAsync(r => r.Id == riId);

                    if (recipeIngredient != null)
                    {
                        //recipeIngredient.Ingredient.Name = "";
                        //recipeIngredient.Ingredient.ProteinsPerItem = 0;
                        //recipeIngredient.Ingredient.CaloriesPerItem = 0;
                        //recipeIngredient.Ingredient.SugarPerItem = 0;

                        _context.Update(recipeIngredient);
                    }
                }
                await _context.SaveChangesAsync();

                Recipe recipe = await _context.Recipe.FirstOrDefaultAsync(r => r.Id == entry.RecipeId);

                return(RedirectToAction(nameof(Index), new { rTitle = recipe.Title }));
            }
            return(RedirectToAction(nameof(Index)));
        }