Validate Field in MVC5 Using Resource File

This article provides an overview of validation in ASP.NET MVC 5 using a Resource file.

Data validation is very important for developing any web application. In ASP.NET MVC, we can easily apply validation to a web application by using the Data Annotation attribute classes to the model class. Data Annotation attribute classes are present in the System.ComponentModel.DataAnnotations namespace and are available to ASP.NET projects like ASP.NET web application and website, ASP.NET MVC, Web forms and also to Entity Framework models.

I will explain validation of controls using built-in validators.

Use the following procedure to create a sample showing how to validate the controls on the client side using validation controls.

  1. Open Visual Studio and select ASP.NET Web Application.



  2. Next Select MVC.



  3. Add a Resource File In My Case named it ErrorMsg.resx.



  4. Set the Access Modifier of the resource file to Public.



  5. Add Messages in Resource file as below:



  6. Right-click the Model and select Add New Item.



  7. Add a class File named EmployeeModel.cs.
  8. Modify the code as below.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.ComponentModel.DataAnnotations;  
    4. using System.Linq;  
    5. using System.Web;  
    6.   
    7.   
    8. namespace MVCDemo.Models  
    9. {  
    10.     public class EmployeeModel  
    11.     {  
    12.         [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpName")]  
    13.         [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$",ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpName")]  
    14.   
    15.         public string Name { getset; }  
    16.          [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpEmail")]  
    17.          [RegularExpression(@"[\w-]+@([\w-]+\.)+[\w-]+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpMail")]          
    18.         public string Email { getset; }  
    19.         [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Address")]  
    20.         public string Address { getset; }  
    21.         [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Technology")]          
    22.         public string Technology { getset; }  
    23.          [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Experience")]    
    24.          [RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpExperience")]     
    25.           
    26.         public decimal Experience { getset; }  
    27.          [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "ContactNo")]  
    28.          [RegularExpression(@"\d+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpContactNo")]            
    29.         public string ContactNo { getset; }  
    30.     }  

  9. Right-click the controller and select "Controller..." | "Add" | "New".



  10. Select "MVC 5 Controller - Empty".



  11. Add a name to the Controller as below EmployeeController.



  12. Modify the code of EmployeeController.cs as below.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Mvc;  
    6.   
    7. namespace MVCDemo.Controllers  
    8. {  
    9.     public class EmployeeController : Controller  
    10.     {  
    11.         //  
    12.         // GET: /Employee/  
    13.         public ActionResult Employee()  
    14.         {  
    15.             MVCDemo.Models.EmployeeModel empModel = new Models.EmployeeModel();  
    16.             return View(empModel);  
    17.         }  
    18.   
    19.         [HttpPost]  
    20.         public ActionResult Employee(MVCDemo.Models.EmployeeModel model)  
    21.         {  
    22.             if (ModelState.IsValid)  
    23.             {  
    24.                 //MVCDemo.Models.EmployeeModel empModel = new Models.EmployeeModel();  
    25.             }  
    26.   
    27.             return View(model);  
    28.         }  
    29.     }  

  13. Right-click the View and add a view.



  14. Name the View as Employee.cshtml.
  15. Modify the Employee.cshtml code as below.
    1. @model MVCDemo.Models.EmployeeModel  
    2.   
    3. @{  
    4.     ViewBag.Title = "Employee";  
    5. }  
    6.   
    7. <h2>Employee</h2>
    8.   
    9.   
    10. <form method="post">  
    11. @using (Html.BeginForm())  
    12. {  
    13.     @Html.AntiForgeryToken()  
    14.     @Html.ValidationSummary()  
    15.     <table>  
    16.         <tr>  
    17.             <td>Name:</td>  
    18.             <td>@Html.TextBox("Name") @Html.ValidationMessage("Name", "*")</td>  
    19.         </tr>  
    20.         <tr>  
    21.             <td>Email:</td>  
    22.             <td>@Html.TextBox("Email") @Html.ValidationMessage("Email", "*")</td>  
    23.         </tr>  
    24.         <tr>  
    25.             <td>Address:</td>  
    26.             <td>@Html.TextBox("Address")@Html.ValidationMessage("Address", "*")</td>  
    27.         </tr>  
    28.         <tr>  
    29.             <td>Technology:</td>  
    30.             <td>@Html.TextBox("Technology")@Html.ValidationMessage("Technology", "*")</td>  
    31.         </tr>  
    32.         <tr>  
    33.             <td>Experience:</td>  
    34.             <td>@Html.TextBox("Experience")@Html.ValidationMessage("Experience", "*")</td>  
    35.         </tr>  
    36.         <tr>  
    37.   
    38.             <td>Contact No.: </td>  
    39.             <td>@Html.TextBox("ContactNo")@Html.ValidationMessage("ContactNo", "*")</td>  
    40.         </tr>  
    41.         <tr>  
    42.             <td colspan="2"><input type="submit" value="Submit" /></td>  
    43.         </tr>  
    44.   
    45.     </table>  
    46. }  
    47.   
    48. </form>  
    49.   
    50. @section Scripts {  
    51. @Scripts.Render("~/Scripts/jquery.validate.js")  
    52. @Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")  
    53. @Scripts.Render("~/Scripts/MicrosoftAjax.debug.js")  
    54. @Scripts.Render("~/Scripts/MicrosoftMvcAjax.debug.js")  
    55. @Scripts.Render("~/Scripts/MicrosoftMvcValidation.debug.js")  

  16. Now click on Internet Explorer and run the program.



  17. The UI will open as below.
  18. Click the submit button without making any entry in a field of the form.


Conclusion

In this article I have explained how to create and validate controls in MVC5 and Razor using a Resource File.

Up Next
    Ebook Download
    View all
    Learn
    View all