2017-09-14 4 views
0

Entity Framework 7을 사용하는 ASP.NET Core 응용 프로그램이 있습니다.
웹 사이트에서 내용을 편집 할 때 경고를 표시하기 위해 데이터 주석을 사용하고 있습니다..NET Core 2.0 EF7 데이터 주석에 경고가 표시되지 않습니다.

루프에서 입력 필드와 해당 경고 범위를 추가 할 때 작동하지 않습니다.
제출을 차단하고있어 경고를 표시하지 않습니다.
내가 잘못하고 있니?

내 코드 :
면도기보기 :

@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");} 
} 

뷰 모델 :

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; } 

    } 
} 

모델 :

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; } 
    } 
} 

답변

0

당신이 당신의 코드가하는 foreach 루프를 사용하고 있기 때문에 중복 HTML로 렌더링 Ds. 이렇게하면 POST에서 모델 바인딩이 실패하게됩니다. for 루프로 바꾸고 그 중 하나를 사용해야합니다.

  1. 는이 솔루션에서와 다른 항목을 구분하는 인덱스를 사용 ASP.NET Core 1.0 POST IEnumerable<T> to controller
  2. 사용 Html.EditorFor 등을이 문서에서 같이 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms#the-input-tag-helper