Validation Message and Validation Summary Tag Helper in ASP.NET MVC Core 3.1

Introduction 

 
 
This blog will explain validation messages and the validation summary tag helper in ASP.NET MVC core. Validation messages and the validation tag helper are similar to ValidationMessage and ValidationMessageFor in asp.net MVC.
 
Complete code.
 

ValidationMessage TagHelper: validation message tag helper targets span element with asp-validation-for attribute which display validation message from modal class validation property.

 

Validation Summary TagHelper: Validation Summary tag helper target the div element. It has asp-validation-sammary attribute which display model validation summary on browser client.

 

ValidationMessage: It is an extension method that is a loosely typed method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.

 

ValidationMessageFor: This method will only display an error if you have configured Data Annotations attribute to the specified property in the model class.

 

ValidationSummary: This ValidationSummary helper method generates an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.

Steps:

Step 1: Create asp.net MVC core project in visual studio 2019 with MVC (Model-View-Controller) template.

Step 2: Right-click on models folder and “Add” class with name student.
 
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace ValidationMessageValidationSummaryTagHelper_Demo.Models  
  5. {  
  6.     public class Student  
  7.     {  
  8.         [Key]  
  9.         public int Id { getset; }  
  10.   
  11.         [Required(ErrorMessage = "Please enter name")]  
  12.         public string Name { getset; }  
  13.   
  14.         [Required(ErrorMessage = "Please choose gender")]  
  15.         public string Gender { getset; }  
  16.   
  17.         [Required(ErrorMessage = "Please choose date of birth")]  
  18.         [Display(Name = "Date of Birth")]  
  19.         [DataType(DataType.Date)]  
  20.         public DateTime DateofBirth { getset; }  
  21.   
  22.         [Required(ErrorMessage = "Please enter address")]  
  23.         [StringLength(255)]  
  24.         public string Address { getset; }  
  25.     }  
  26. }  
 

Step 3: Open HomeController or add HomeController if you don’t have it. Add the action method Create in controller class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.Extensions.Logging;  
  8. using ValidationMessageValidationSummaryTagHelper_Demo.Models;  
  9.   
  10. namespace ValidationMessageValidationSummaryTagHelper_Demo.Controllers  
  11. {  
  12.     public class HomeController : Controller  
  13.     {  
  14.         public IActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         public IActionResult Create()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         [HttpPost]  
  25.         public IActionResult Create(Student student)  
  26.         {  
  27.             if (ModelState.IsValid)  
  28.             {  
  29.   
  30.             }  
  31.             return View();  
  32.         }  
  33.     }  
  34. }  

Step 4 Right click on create action method and “Add” create view and write following code.

  1. @model ValidationMessageValidationSummaryTagHelper_Demo.Models.Student  
  2. @{  
  3.     ViewData["Title"] = "Create";  
  4. }  
  5.   
  6. <div class="card">  
  7.     <div class="card-header">  
  8.         <h4>Student Details</h4>  
  9.     </div>  
  10.     <div class="card-body">  
  11.         <div asp-validation-summary="All"></div>  
  12.         <form asp-action="Create">  
  13.             <div class="form-group">  
  14.                 <label asp-for="Name" class="label-control"></label>  
  15.                 <input asp-for="Name" class="form-control" />  
  16.                 <span asp-validation-for="Name" class="text-danger"></span>  
  17.             </div>  
  18.             <div class="form-group">  
  19.                 <label asp-for="Gender" class="label-control"></label>  
  20.                 <select asp-for="Gender" class="custom-select">  
  21.                     <option value="">Choose Geneder</option>  
  22.                     <option value="Male">Male</option>  
  23.                     <option value="Female">Female</option>  
  24.                 </select>  
  25.                 <span asp-validation-for="Gender" class="text-danger"></span>  
  26.             </div>  
  27.             <div class="form-group">  
  28.                 <label asp-for="DateofBirth" class="label-control"></label>  
  29.                 <input asp-for="DateofBirth" class="form-control" />  
  30.                 <span asp-validation-for="DateofBirth" class="text-danger"></span>  
  31.             </div>  
  32.             <div class="form-group">  
  33.                 <label asp-for="Address" class="label-control"></label>  
  34.                 <textarea asp-for="Address" class="form-control"></textarea>  
  35.                 <span asp-validation-for="Address" class="text-danger"></span>  
  36.             </div>  
  37.             <div>  
  38.                 <button type="submit" class="btn btn-sm btn-primary rounded-0">Submit</button>  
  39.             </div>  
  40.         </form>  
  41.     </div>  
  42. </div>  
The Validation Summary Tag Helper is used to display a summary of validation messages. The asp-validation-summary attribute value can be any of the following:
 

asp-validation-summary

Validation messages displayed

ValidationSummary.All

Property and model level

ValidationSummary.ModelOnly

Model

ValidationSummary.None

None

 
  1. <div asp-validation-summary="All"></div>  
  2. <div asp-validation-summary="ModelOnly"></div>  
  3. <div asp-validation-summary="None"></div>  

Step 5: Add the following CSS in site.css file under wwwroot folder.

  1. .field-validation-error {  
  2.     color: #e80c4d;  
  3.     font-weight: bold;  
  4. }  
  5. .field-validation-valid {  
  6.     display: none;  
  7. }  
  8. input.input-validation-error {  
  9.     border: 1px solid #e80c4d;  
  10. }  
  11. .validation-summary-errors {  
  12.     color: #e80c4d;  
  13.     font-weight: bold;  
  14.     font-size: 1.1em;  
  15. }  
  16. .validation-summary-valid {  
  17.     display: none;  
  18. }  
Step 6: Build and run your application Ctrl+F5