簡體   English   中英

.NET Core 2.0 EF7數據注釋未顯示警告

[英].NET Core 2.0 EF7 Data annotations not showing warning

我有一個帶有實體框架7的ASP.NET Core應用程序。
在網站上編輯內容時,我正在使用數據注釋顯示警告。

在循環中添加輸入字段及其各自的警告范圍時,此操作無效。
它阻止了提交,沒有顯示任何警告。
難道我做錯了什么?

我的代碼:
剃刀視圖:

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

    }
}

模型:

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

因為您使用的是foreach循環,所以您的代碼將呈現具有重復ID的HTML。 這將導致模型綁定在POST上失敗。 您需要將其替換為for循環,然后再選擇其中一個;

  1. 使用索引來區分不同的項目,例如此解決方案中的內容: ASP.NET Core 1.0將IEnumerable <T>傳遞給控制器
  2. 使用Html.EditorFor,例如本文檔中的: https ://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/working-with-forms#the-input-tag-helper

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM