Validation Message And Validation Summary In ASP.NET MVC

In this article you will learn about validation message and validation summary in ASP.NET MVC.

Introduction

 
This article will explain validation message and validation summary in ASP.NET MVC. We will understand it with examples.
  1. ValidationMessage
  2. ValidationMessageFor
  3. ValidationSummary
Step 1
 
Open Visual Studio 2015 or a version of your choice and create a table with some data.
 
Step 2
 
In Visual Studio, choose a web application project and give an appropriate name to your project.
 
Validation Message And Validation Summary In ASP.NET MVC
 
Step 3
 
Select "empty template", check on the MVC checkbox, and click OK.
 
Validation Message And Validation Summary In ASP.NET MVC
 
Step 4
 
Right click on models folder and “Add” Employee class, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.    
  7. namespace MvcValidationMessage_Demo.Models  
  8. {  
  9.     public class Employee  
  10.     {  
  11.         public int Id { getset; }  
  12.    
  13.         [Required(ErrorMessage = "Please enter name")]  
  14.         public string Name { getset; }  
  15.    
  16.         [Required(ErrorMessage = "Please enter position")]  
  17.         public string Position { getset; }  
  18.    
  19.         [Required(ErrorMessage = "Please enter salary")]  
  20.         public int Salary { getset; }  
  21.     }  
  22. }  
Step 5
 
Right-click on the Controllers folder and add a controller.
 
Validation Message And Validation Summary In ASP.NET MVC
 
A window will appear. Choose MVC5 Controller-Empty and click "Add".
 
Validation Message And Validation Summary In ASP.NET MVC
 
After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.
 
Validation Message And Validation Summary In ASP.NET MVC
 
Home Controller Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcValidationMessage_Demo.Models;  
  7.    
  8. namespace MvcValidationMessage_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         // GET: Home  
  13.         public ActionResult Index()  
  14.         {  
  15.             return View();  
  16.         }  
  17.    
  18.         public ActionResult Create()  
  19.         {  
  20.             return View();  
  21.         }  
  22.    
  23.         [HttpPost]  
  24.         public ActionResult Create(Employee employee)  
  25.         {  
  26.             if (ModelState.IsValid)  
  27.             {  
  28.    
  29.             }  
  30.             return View();  
  31.         }  
  32.     }  
  33. }  
Step 6
 
Right-click the Index method in HomeController. The "Add View" window will appear with default index name checked (use a Layout page). Click on "Add".
 
Validation Message And Validation Summary In ASP.NET MVC
 
The Html.ValidationMessage() 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.
 
Validation Method Signature
  1. MvcHtmlString ValidateMessage(string modelName, string validationMessage, object htmlAttributes)  
  2.   
  3. @model MvcValidationMessage_Demo.Models.Employee  
  4.    
  5. @{  
  6.     ViewBag.Title = "Create";  
  7. }  
  8.    
  9. <h4>Create New Employee</h4>  
  10. @using (Html.BeginForm())  
  11. {  
  12.     <div class="form-group">  
  13.         @Html.LabelFor(m=>m.Name)  
  14.         @Html.TextBoxFor(m => m.Name, new {@class = "form-control"})  
  15.         @Html.ValidationMessage("Name","Please enter name","text-danger")  
  16.     </div>  
  17.     <div class="form-group">  
  18.         @Html.LabelFor(m => m.Position)  
  19.         @Html.TextBoxFor(m => m.Position, new { @class = "form-control" })  
  20.         @Html.ValidationMessage("Position""Please enter position""text-danger")  
  21.     </div>  
  22.     <div class="form-group">  
  23.         @Html.LabelFor(m => m.Salary)  
  24.         @Html.TextBoxFor(m => m.Salary, new { @class = "form-control" })  
  25.         @Html.ValidationMessage("Salary","Please enter salary","text-danger")  
  26.     </div>  
  27.     <div class="form-group">  
  28.         <button type="submit" class="btn btn-primary">Submit</button>  
  29.     </div>  
  30. }  
Validation Message And Validation Summary In ASP.NET MVC
 
In ValidationMessage, the first parameter is modelName method, which is a lambda expression to specify a property for which we want to show the error message. The second parameter validationMassage and the third parameter is for html attributes like css, style etc.
 
ValidationMessageFor
 
The Html.ValidationMessageFor() is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
 
ValidationMessageFor signature
  1. MvcHtmlString ValidateMessage(Expression<Func<dynamic, TProperty>> expression, string validationMessage, object htmlAttributes)  
  2.   
  3. @model MvcValidationMessage_Demo.Models.Employee  
  4.    
  5. @{  
  6.     ViewBag.Title = "Create";  
  7. }  
  8.    
  9. <h4>Create New Employee</h4>  
  10. @using (Html.BeginForm())  
  11. {  
  12.     <div class="form-group">  
  13.         @Html.LabelFor(m=>m.Name)  
  14.         @Html.TextBoxFor(m => m.Name, new {@class = "form-control"})  
  15.         @Html.ValidationMessageFor(m=>m.Name)  
  16.     </div>  
  17.     <div class="form-group">  
  18.         @Html.LabelFor(m => m.Position)  
  19.         @Html.TextBoxFor(m => m.Position, new { @class = "form-control" })  
  20.         @Html.ValidationMessageFor(m => m.Position)  
  21.     </div>  
  22.     <div class="form-group">  
  23.         @Html.LabelFor(m => m.Salary)  
  24.         @Html.TextBoxFor(m => m.Salary, new { @class = "form-control" })  
  25.         @Html.ValidationMessageFor(m => m.Salary)  
  26.     </div>  
  27.     <div class="form-group">  
  28.         <button type="submit" class="btn btn-primary">Submit</button>  
  29.     </div>  
  30. }  
Validation Message And Validation Summary In ASP.NET MVC
 
The ValidationMessageFor() method will only display an error if you have configured DataAnnotations attribute to the specified property in the model class.
 
ValidationSummary
 
The ValidationSummary helper method generates an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.
 
The ValidationSummary can be used to display all the error messages for all the fields. It can also be used to display custom error messages.
 
Displaying all validation errors at one place using validation summary HTML helper
 
To display all errors at one place, use ValidationSummary() HTML helper.
  1. @Html.ValidationSummary(false"Please fix the following error and then submit the form")  
  2.   
  3. @model MvcValidationMessage_Demo.Models.Employee  
  4.    
  5. @{  
  6.     ViewBag.Title = "Create";  
  7. }  
  8.    
  9. <h4>Create New Employee</h4>  
  10. @using (Html.BeginForm())  
  11. {  
  12.     <div class="form-group">  
  13.         @Html.LabelFor(m=>m.Name)  
  14.         @Html.TextBoxFor(m => m.Name, new {@class = "form-control"})  
  15.         @Html.ValidationMessageFor(m=>m.Name)  
  16.     </div>  
  17.     <div class="form-group">  
  18.         @Html.LabelFor(m => m.Position)  
  19.         @Html.TextBoxFor(m => m.Position, new { @class = "form-control" })  
  20.         @Html.ValidationMessageFor(m => m.Position)  
  21.     </div>  
  22.     <div class="form-group">  
  23.         @Html.LabelFor(m => m.Salary)  
  24.         @Html.TextBoxFor(m => m.Salary, new { @class = "form-control" })  
  25.         @Html.ValidationMessageFor(m => m.Salary)  
  26.     </div>  
  27.     <div class="form-group">  
  28.         <button type="submit" class="btn btn-primary">Submit</button>  
  29.     </div>  
  30.     @Html.ValidationSummary(false"Please fix the following error and then submit the form")  
  31. }  
Validation Message And Validation Summary In ASP.NET MVC