0

I have an ASP.NET Core application with Entity framework 7.
I am using Data annotations to display warnings when editing content on the website.

When adding the input field and their respective warning span in a loop this didn't work.
It is blocking the submit, it is not showing any warning.
Am I doing something wrong?

my code:
Razor view:

@model Receptenboek.Models.RecipeViewModels.RecipeEdit
@{
    ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>Recipe</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Recipe.ID" />
            <input type="hidden" asp-for="Recipe.Ingredients" />
            <div class="form-group">
                <label asp-for="Recipe.Name" class="control-label"></label>
                <input asp-for="Recipe.Name" class="form-control" />
                <span asp-validation-for="Recipe.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <h3 asp-for="Book" class="control-label">Book</h3>
                <select asp-for="Recipe.RecipeBookID" asp-items="@(new SelectList(Model.Books, "ID", "BookName", Model.Recipe.RecipeBookID))"></select><br />
                <span asp-validation-for="Books" class="text-danger"></span>
            </div>
            <div id="fields" class="form-actions">

                @foreach (Recipe_Ingredient ri in Model.Recipe.Ingredients)
            {
                    <div id="IngredientDiv">
                        <label class="control-label">Ingredient name </label><br />
                        <select name="ingredients" asp-for="@ri.IngredientID" asp-items="@(new SelectList(Model.Ingredients, "ID", "Name"))"></select><br />
                        <label class="control-label">Amount </label>
                        <input name="Amount" class="form-control" asp-for="@ri.Amount" asp-route-amounts="@ViewData["amounts"]" />
                        <span asp-validation-for="@ri.Amount" class="text-danger"></span>
                        <hr />
                    </div>
                }
            </div>
            <span class="text-danger">@ViewData["Warning"]</span><br/>
            <a onclick="duplicate()">Add Ingredient</a>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Back to List</a>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
} 

ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Receptenboek.Models.RecipeViewModels
{
    public class RecipeEdit
    {
        public Recipe Recipe { get; set; }
        public ICollection<Ingredient> Ingredients { get; set; }
        public ICollection<RecipeBook> Books { get; set; }

    }
}

Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Receptenboek.Models
{
    public class Recipe
    {
        public int ID { get; set; }
        [Required]
        [Display(Name = "Book")]
        public int RecipeBookID { get; set; }
        [Required]
        [Display(Name = "Recipe Name")]
        public string Name { get; set; }

        public ICollection<Recipe_Ingredient> Ingredients { get; set; }
        public RecipeBook Book { get; set; }
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
mark DE Jong
  • 257
  • 2
  • 17

1 Answers1

0

Because you are using a foreach loop, your code will render HTML with duplicate IDs. This will cause the model binding to fail on POST. You need to replace it with a for loop and then either;

  1. Use the index to distinguish the different items such as in this solution: ASP.NET Core 1.0 POST IEnumerable<T> to controller
  2. Use Html.EditorFor such as in this documentation: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms#the-input-tag-helper
joakimriedel
  • 1,801
  • 12
  • 27