From 000cead8774d532bce340d849133e081b4da9e78 Mon Sep 17 00:00:00 2001 From: franc Date: Mon, 5 May 2025 12:01:53 +0200 Subject: [PATCH] - Replaced `foreach` loop with `for` loop for better index-based access to instructions. - Added logic to dynamically retrieve media files for each instruction using `Request.Form.Files`. - Ensured that media files are correctly associated with their respective instructions during recipe creation. --- .../Controller/Recipe/RecipeController.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs index d7abfaa..4477962 100644 --- a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs +++ b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs @@ -230,14 +230,23 @@ if (model.InstructionViewModel?.Instructions != null) { - foreach (var instruction in model.InstructionViewModel.Instructions) + for (var i = 0; i < model.InstructionViewModel.Instructions.Count; i++) { + var instruction = model.InstructionViewModel.Instructions[i]; if (!string.IsNullOrWhiteSpace(instruction.Description)) { + var fileKey = $"InstructionViewModel.Instructions[{i}].MediaFile"; + IFormFile? imageFile = null; + + if (Request.Form.Files.Any(f => f.Name == fileKey)) + { + imageFile = Request.Form.Files[fileKey]; + } + await _instructionRepository.CreateInstructionWithImageToRecipeAsync( recipe.Id, instruction.Description, - null); + imageFile); } } }