public IActionResult Delete(int id)
        {
            var oldIngredient = _ingredientData.GetById(id);

            if (oldIngredient == null)
            {
                return(NotFound());
            }
            _ingredientData.Delete(id);
            _ingredientData.Commit();
            return(Ok());
        }
示例#2
0
        public IActionResult OnPost(int ingredientId)
        {
            var ingredient = ingredientData.Delete(ingredientId);

            ingredientData.Commit();

            if (ingredient == null)
            {
                return(RedirectToPage("./NotFound"));
            }
            return(RedirectToPage("./List"));
        }
示例#3
0
        public async Task <IActionResult> OnPostAsync()
        {
            var userId = userManager.GetUserId(User);

            if (!ModelState.IsValid)
            {
                Cuisines = htmlHelper.GetEnumSelectList <CuisineType>();
                return(Page());
            }

            if (NewImage != null)
            {
                var image = await imageData.UploadImageAsync(NewImage);

                Recipe.ImageUrl = image;
            }

            // Update existing Ingredients
            var savedIngredients = new List <int>();

            foreach (var ingredient in Recipe.Ingredients)
            {
                if (ingredient.Id > 0)
                {
                    savedIngredients.Add(ingredient.Id);
                    ingredientData.Update(ingredient);
                }
            }

            // Delete removed ingredients
            var originalRecipe     = recipeData.GetById(Recipe.Id);
            var deletedIngredients = originalRecipe.Ingredients.Where(i => !savedIngredients.Contains(i.Id));

            foreach (var deletedIngredient in deletedIngredients)
            {
                ingredientData.Delete(deletedIngredient.Id);
            }

            // Update Recipe
            recipeData.Update(Recipe);
            TempData["Message"] = "Recipe Updated.";
            recipeData.Commit();
            return(RedirectToPage("./Detail", new { recipeId = Recipe.Id }));
        }