简体   繁体   English

@ Html.ValidationSummary(true,“请修复错误”)不显示“请修复错误”消息

[英]@Html.ValidationSummary(true, “Please fix errors”) not displaying “Please fix errors” message

I was following along with a tutorial on how to implement validation in my MVC web application. 我跟随着有关如何在MVC Web应用程序中实现验证的教程。 Below is a snippet of my View for creating a new Student: 以下是用于创建新学生的“我的视图”的摘要:

<div class="form-horizontal">
    <h4>Student</h4>
    <hr />
    @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
    </div>

Here is a snippet of my controller code: 这是我的控制器代码的一个片段:

 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Data.Entity;
 using System.Linq;
 using System.Net;
 using System.Web;
 using System.Web.Mvc;
 using ContosoUniversity.DAL;
 using ContosoUniversity.Models;

 namespace ContosoUniversity.Controllers
 {
     public class StudentController : Controller
     {
          private SchoolContext db = new SchoolContext();
          public ActionResult Create()
          {
                return View();
          }

         [HttpPost]
         [ValidateAntiForgeryToken]
         public ActionResult Create([Bind(Include = 
         "ID,LastName,FirstMidName,EnrollmentDate")] Student student)
         {

             if (ModelState.IsValid)
             {
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
             }

             return View(student);

        }

My @Html.ValidationMessageFor(model => model.LastName) works without a problem. 我的@Html.ValidationMessageFor(model => model.LastName)可以正常工作。 Even @Html.ValidationSummary(false) works to displays my field errors, as expected, but I don't want my field errors displayed. 甚至@Html.ValidationSummary(false)可以按预期显示我的字段错误,但是我不希望显示我的字段错误。 When I change false to true and include my custom message, the message does not show up when I run my code in the browser. 当我将false更改为true并包含我的自定义消息时,在浏览器中运行代码时不会显示该消息。 I cannot figure out what is going on, and I've tried researching the answer on the web, but there is no answer that makes sense to me, even though I'm sure it has been answered somewhere. 我无法弄清楚发生了什么,我尝试在网络上研究答案,但是即使我确定某个地方已经找到答案,也没有对我有意义的答案。 Can someone please provide me with a "layman's terms" solution to this problem? 有人可以为我提供“外行条款”解决方案吗?

To showing message change validationSummary as true and to display custom error message to non field use ModelState.AddModelError("","Error message") without key. 要将消息更改validationSummary显示为true,并向非字段显示自定义错误消息,请使用不带键的ModelState.AddModelError(“”,“ Error message”)。 validationSummary true case non key model error added in unordered list and field model error messages showing in ValidationMessageFor. validationSummary在ValidationMessageFor中显示的无序列表和字段模型错误消息中添加了真案例非关键模型错误。

ModelState.AddModelError("", "Error Message"); ModelState.AddModelError(“”,“错误消息”);

像这样在第二个参数中传递所需的摘要语句。

@Html.ValidationSummary(true, "Please check form errors", new { @class = "text-danger" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM