0

I was following along with a tutorial on how to implement validation in my MVC web application. 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. Even @Html.ValidationSummary(false) works to displays my field errors, as expected, but I don't want my field errors displayed. 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. 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?

Michael
  • 1
  • 1
  • Michael, what is not working? Can you state what exactly are trying to accomplish? Can you also please post your controller code? It is the controller code that will determine what errors are generated? – arod Dec 23 '18 at 15:23
  • Will do. Just edited my question. I'm trying to create a new Student (for a University enrollment application). I want the field errors to show up (not a problem there), then I want validation summary to just be that: a summary with no field errors. – Michael Dec 23 '18 at 17:24
  • Please check [this](https://stackoverflow.com/questions/2818219/asp-net-mvc-html-validationsummarytrue-does-not-display-model-errors) link, it should help you. – ecstatic Dec 25 '18 at 13:29

2 Answers2

0

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 case non key model error added in unordered list and field model error messages showing in ValidationMessageFor.

ModelState.AddModelError("", "Error Message");

  • See, when I saw that solution, it made sense, so I tried it, and I still got nothing, so I do not know if I'm implementing it correctly or if there's something else going on. – Michael Dec 23 '18 at 17:24
0

Pass in the summary statement you want, in the second parameter, like this.

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