Understanding Validation In MVC - Part 4

This article focuses on implementing validation summary in MVC and continuation of previous article,

Validation Summary

Validation Summary helps you to display all your validation error messages in the summary box.
It will display all your validation errors in unordered list format. excludePropertyErrors: true to have the summary display model-level errors only, or false.

We have the following steps to implement it,

Step 1: Define the validation summary inside your form as in the following.

Code

Step 2: So my final View code will look like the following code snippet.

  1. @model DropdownGrid.Models.StudentDetailsModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8. <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>  
  9. <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>  
  10. <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>  
  11. <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>  
  12. @Styles.Render("~/Content/css")  
  13. <script type="text/javascript">  
  14.   
  15.     function foo(result) {  
  16.   
  17.         $('#dvCategoryResults').html(result);  
  18.     }  
  19.   
  20. </script>  
  21.   
  22. <br>  
  23. <br>  
  24. <br>  
  25. <br>  
  26.   
  27.   
  28.   
  29. <fieldset>  
  30.    @using (Html.BeginForm())  
  31.    {  
  32.       
  33.          
  34.        @Html.ValidationSummary(false)  
  35.   
  36.         <table>  
  37.             <tr>  
  38.                 <td>  
  39.                     @Html.LabelFor(M => M.SelectedStudent.Id)  
  40.                 </td>  
  41.                 <td>  
  42.                     @Html.TextBoxFor(M => M.SelectedStudent.Id)  
  43.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Id,"*")  
  44.                 <td>  
  45.             </tr>  
  46.             <tr>  
  47.                 <td>  
  48.                     @Html.LabelFor(M => M.SelectedStudent.Name)  
  49.                 </td>  
  50.                 <td>  
  51.                     @Html.TextBoxFor(M => M.SelectedStudent.Name)  
  52.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Name, "*")  
  53.                 <td>  
  54.             </tr>  
  55.             <tr>  
  56.                 <td>  
  57.                     @Html.LabelFor(M => M.SelectedStudent.Address)  
  58.                 </td>  
  59.                 <td>  
  60.                     @Html.TextBoxFor(M => M.SelectedStudent.Address)  
  61.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Address, "*")  
  62.                 <td>  
  63.             </tr>  
  64.             <tr>  
  65.                 <td>  
  66.                     @Html.LabelFor(M => M.SelectedStudent.Class)  
  67.                 </td>  
  68.                 <td>  
  69.                     @Html.TextBoxFor(M => M.SelectedStudent.Class)  
  70.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Class, "*")  
  71.                 <td>  
  72.             </tr>  
  73.             <tr>  
  74.                 <td>  
  75.                     @Html.LabelFor(M => M.SelectedStudent.Section)  
  76.                 </td>  
  77.                 <td>  
  78.                     @Html.TextBoxFor(M => M.SelectedStudent.Section)  
  79.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Section, "*")  
  80.                 <td>  
  81.             </tr>  
  82.             <tr>  
  83.                 <td>  
  84.                     @Html.LabelFor(M => M.SelectedStudent.Email)  
  85.                 </td>  
  86.                 <td>  
  87.                     @Html.TextBoxFor(M => M.SelectedStudent.Email)  
  88.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Email, "*")  
  89.                 <td>  
  90.             </tr>  
  91.             <tr>  
  92.                 <td>  
  93.                     @Html.LabelFor(M => M.SelectedStudent.Percentage)  
  94.                 </td>  
  95.                 <td>  
  96.                     @Html.TextBoxFor(M => M.SelectedStudent.Percentage)  
  97.                     @Html.ValidationMessageFor(M => M.SelectedStudent.Percentage, "*")  
  98.                 <td>  
  99.             </tr>  
  100.             <tr>  
  101.                 <td>  
  102.                     @Html.LabelFor(M => M.SelectedStudent.AdmissionDate)  
  103.                 </td>  
  104.                 <td>  
  105.                     @Html.TextBoxFor(M => M.SelectedStudent.AdmissionDate)  
  106.                     @Html.ValidationMessageFor(M => M.SelectedStudent.AdmissionDate, "*")  
  107.                 <td>  
  108.             </tr>  
  109.   
  110.         </table>  
  111.         <input type="submit" value="Submit" />  
  112.     }  
  113. </fieldset>  
Step 3: Execute the project and you will find all the validations are visible in validation summary box

Enter detail

Hope you understood validation summary. I have explained some more things in Validation and advanced topic in Validation summary. 

Up Next
    Ebook Download
    View all
    Learn
    View all