【问题标题】:.NET Core 2.0 EF7 Data annotations not showing warning.NET Core 2.0 EF7 数据注释未显示警告
【发布时间】:2017-09-20 15:51:54
【问题描述】:

我有一个带有实体框架 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");}
} 

视图模型:

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

【问题讨论】:

    标签: c# asp.net-core-mvc entity-framework-core


    【解决方案1】:

    因为您使用的是 foreach 循环,所以您的代码将呈现带有重复 ID 的 HTML。这将导致模型绑定在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 2016-06-25
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多