Server Side Validation On ASP.NET MVC Kendo UI

server side validation

Add a Class to the Model Folder named “ClsSample”.

add new class

Class for Validation Attributes

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6.   
  7. namespace TelerikMvcApp1.Models  
  8. {  
  9.     public class ClsSample  
  10.     {         
  11.         public int RowId  
  12.         {  
  13.             set;  
  14.             get;  
  15.         }  
  16.   
  17.         [Required(ErrorMessage="* Name")]  
  18.         [Display(Name="Name")]  
  19.         [DataType(DataType.Text)]  
  20.         public string Name  
  21.         {  
  22.             set;  
  23.             get;  
  24.         }  
  25.   
  26.         [Required(ErrorMessage="* Age")]  
  27.         [Display(Name="Age")]  
  28.         [DataType(DataType.PhoneNumber)]  
  29.         [StringLength(3)]  
  30.         public int? Age  
  31.         {  
  32.             set;  
  33.             get;  
  34.         }  
  35.   
  36.         [Required(ErrorMessage="* Mobile")]  
  37.         [Display(Name="Mobile")]  
  38.         [DataType(DataType.PhoneNumber)]  
  39.         [StringLength(10)]  
  40.         public long? Mobile  
  41.         {  
  42.             set;  
  43.             get;  
  44.         }  
  45.   
  46.         [Required(ErrorMessage="* EmailID")]  
  47.         [Display(Name="EmailID")]  
  48.         [DataType(DataType.EmailAddress)]  
  49.         [RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Address")]  
  50.         public string EmailId  
  51.         {  
  52.             set;  
  53.             get;  
  54.         }  
  55.   
  56.         [Required(ErrorMessage="* Select Gender")]  
  57.         [Display(Name="Gender")]  
  58.         public string Gender  
  59.         {  
  60.             set;  
  61.             get;  
  62.         }  
  63.   
  64.         public string MaritalStatus  
  65.         {  
  66.             set;  
  67.             get;  
  68.         }  
  69.   
  70.         [Required(ErrorMessage="* Father Name")]  
  71.         [Display(Name="FatherName")]  
  72.         [DataType(DataType.Text)]  
  73.         public string FatherName  
  74.         {  
  75.             set;  
  76.             get;  
  77.         }  
  78.   
  79.         [Required(ErrorMessage = "* Mother Name")]  
  80.         [Display(Name="Mother Name")]  
  81.         [DataType(DataType.Text)]  
  82.         public string MotherName  
  83.         {  
  84.             set;  
  85.             get;  
  86.         }  
  87.   
  88.         [Required(ErrorMessage = "* Address")]  
  89.         [Display(Name="Address")]  
  90.         [DataType(DataType.Text)]  
  91.         public string Address  
  92.         {  
  93.             set;  
  94.             get;  
  95.         }  
  96.   
  97.         [Required(ErrorMessage = "* PinCode")]  
  98.         [Display(Name="Pin Code")]  
  99.         [DataType(DataType.PostalCode)]  
  100.         [Range(0,6)]  
  101.         public long? PinCode  
  102.         {  
  103.             set;  
  104.             get;  
  105.         }  
  106.   
  107.         public string NearByLegend  
  108.         {  
  109.             set;  
  110.             get;  
  111.         }  
  112.   
  113.         [Required(ErrorMessage="Select Country")]  
  114.         public int CountryId  
  115.         {  
  116.             set;  
  117.             get;  
  118.         }  
  119.   
  120.         public string CountryName  
  121.         {  
  122.             set;  
  123.             get;  
  124.         }  
  125.   
  126.         public int? StateId  
  127.         {  
  128.             set;  
  129.             get;  
  130.         }  
  131.   
  132.         public string StateName  
  133.         {  
  134.             set;  
  135.             get;  
  136.         }  
  137.   
  138.         public int? CityId  
  139.         {  
  140.             set;  
  141.             get;  
  142.         }  
  143.   
  144.         public string CityName  
  145.         {  
  146.             set;  
  147.             get;  
  148.         }                
  149.     }  
  150. }  
Add Controller and Action Name to The Application.

add new controller

Create an ActionResult and Add View...

Add view

type view name

At the View Bind the Model Class To The View.

View Bind the Model Class

Add your other code to populate The View With Data(Optional).
  1. public ActionResult Validate()  
  2. {  
  3.     #region -- Creating a Form For Server side Data Validation --    
  4.   
  5.     var CountryTemp = (from x in objContext.TblCountries  
  6.                        select new ClsSample  
  7.                        {  
  8.                            CountryId = x.CountryId,  
  9.                            CountryName = x.CountryName  
  10.                        }).ToList();  
  11.   
  12.     ViewData["customers"] = CountryTemp;  
  13.   
  14.     ViewBag.CName2 = new SelectList(CountryTemp, "CountryId""CountryName");  
  15.   
  16.     return View();  
  17.  
  18.     #endregion  
  19. }  
  20.   
  21. [HttpPost]  
  22. public ActionResult Validate(ClsSample obj)  
  23. {  
  24.     return View();  
  25. }  
At the View write the following:
  1. @Html.Kendo().TextBoxFor(model=>model.Name).Name (Bind The Model Property To The TextBox)  
  2. @Html.ValidationMessageFor(X=>X.Name) (Bind the Model Property to the Validation Message For)  
View Part
  1. @model TelerikMvcApp1.Models.ClsSample  
  2.   
  3. @{  
  4.     ViewBag.Title = "Server Side Validate";  
  5. }  
  6.   
  7. @section featured {  
  8.     <div class="featured">  
  9.         <div class="content-wrapper">  
  10.             <hgroup class="title">  
  11.                 <h1>@ViewBag.Title.</h1>  
  12.                 <h2>@ViewBag.Message</h2>  
  13.             </hgroup>  
  14.             <p>  
  15.                 This Is Form To Register New Employee  
  16.             </p>  
  17.         </div>  
  18.     </div>  
  19. }  
  20.   
  21. @using (@Html.BeginForm())  
  22. {  
  23.     <table>  
  24.         <tr>  
  25.             <td>Enter Name</td>  
  26.             <td>:</td>  
  27.             <td>@Html.Kendo().TextBoxFor(model=>model.Name).Name("txtName")</td>  
  28.             <td>@Html.ValidationMessageFor(X=>X.Name)</td>  
  29.         </tr>  
  30.         <tr>  
  31.             <td>Enter Age</td>  
  32.             <td>:</td>  
  33.             <td>@Html.Kendo().TextBoxFor(model=>model.Age).Name("txtAge")</td>  
  34.             <td>@Html.ValidationMessageFor(X=>X.Age)</td>  
  35.         </tr>  
  36.         <tr>  
  37.             <td>Enter Mobile</td>  
  38.             <td>:</td>  
  39.             <td>@Html.Kendo().TextBoxFor(model=>model.Mobile).Name("txtMobile")</td>  
  40.             <td>@Html.ValidationMessageFor(model=>model.Mobile)</td>  
  41.         </tr>  
  42.         <tr>  
  43.             <td>Enter EmailID</td>  
  44.             <td>:</td>  
  45.             <td>@Html.Kendo().TextBoxFor(model=>model.EmailId).Name("txtEmailID")</td>  
  46.             <td>@Html.ValidationMessageFor(model=>model.EmailId)</td>  
  47.         </tr>  
  48.         <tr>  
  49.             <td>Select Gender</td>  
  50.             <td>:</td>  
  51.             <td>@Html.Kendo().RadioButton().Name("Male").Label("Male").HtmlAttributes(new { name = "a", value = "M" })</td>  
  52.             <td>@Html.Kendo().RadioButton().Name("FeMale").Label("FeMale").HtmlAttributes(new { name = "a", value = "F" })</td>  
  53.             <td></td>  
  54.         </tr>  
  55.         <tr>  
  56.             <td>Check Marital Status</td>  
  57.             <td>:</td>  
  58.             <td>@Html.Kendo().CheckBox().Name("chkMarried").Label("Married").HtmlAttributes(new { name = "A", value = "Yes" })</td>  
  59.             <td></td>  
  60.         </tr>  
  61.         <tr>  
  62.             <td>Select Country</td>  
  63.             <td>:</td>  
  64.             <td>@Html.Kendo().DropDownList().Name("ddlCountry").BindTo(ViewBag.CName2)</td>  
  65.             <td></td>  
  66.         </tr>  
  67.         <tr>  
  68.             <td>Enter Pin Code</td>  
  69.             <td>:</td>  
  70.             <td>@Html.Kendo().TextBoxFor(X=>X.PinCode).Name("txtPinCode")</td>  
  71.             <td></td>  
  72.         </tr>  
  73.         <tr>  
  74.             <td>Test Data</td>  
  75.             <td>:</td>  
  76.             <td><input type="submit" value="Save" class="k-primary" width="200" /></td>  
  77.         </tr>  
  78.     </table>   
  79. }  
  1. <script>  
  2.    $(function () {  
  3.       $("form").kendoValidator();  
  4.    });  
  5. </script>  
And finally write this script at the View Page:
  1. <script>  
  2.    $(function () {  
  3.       $("form").kendoValidator();  
  4.    });  
  5. </script>  
Run and see the output.