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.
- Open Visual Studio and select ASP.NET Web Application.
- Next Select MVC.
- Add a Resource File In My Case named it ErrorMsg.resx.
- Set the Access Modifier of the resource file to Public.
- Add Messages in Resource file as below:
- Right-click the Model and select Add New Item.
- Add a class File named EmployeeModel.cs.
- Modify the code as below.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
-
-
- namespace MVCDemo.Models
- {
- public class EmployeeModel
- {
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpName")]
- [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$",ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpName")]
-
- public string Name { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpEmail")]
- [RegularExpression(@"[\w-]+@([\w-]+\.)+[\w-]+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpMail")]
- public string Email { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Address")]
- public string Address { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Technology")]
- public string Technology { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Experience")]
- [RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpExperience")]
-
- public decimal Experience { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "ContactNo")]
- [RegularExpression(@"\d+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpContactNo")]
- public string ContactNo { get; set; }
- }
- }
- Right-click the controller and select "Controller..." | "Add" | "New".
- Select "MVC 5 Controller - Empty".
- Add a name to the Controller as below EmployeeController.
- Modify the code of EmployeeController.cs as below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MVCDemo.Controllers
- {
- public class EmployeeController : Controller
- {
-
-
- public ActionResult Employee()
- {
- MVCDemo.Models.EmployeeModel empModel = new Models.EmployeeModel();
- return View(empModel);
- }
-
- [HttpPost]
- public ActionResult Employee(MVCDemo.Models.EmployeeModel model)
- {
- if (ModelState.IsValid)
- {
-
- }
-
- return View(model);
- }
- }
- }
- Right-click the View and add a view.
- Name the View as Employee.cshtml.
- Modify the Employee.cshtml code as below.
- @model MVCDemo.Models.EmployeeModel
-
- @{
- ViewBag.Title = "Employee";
- }
-
- <h2>Employee</h2>
-
-
- <form method="post">
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- @Html.ValidationSummary()
- <table>
- <tr>
- <td>Name:</td>
- <td>@Html.TextBox("Name") @Html.ValidationMessage("Name", "*")</td>
- </tr>
- <tr>
- <td>Email:</td>
- <td>@Html.TextBox("Email") @Html.ValidationMessage("Email", "*")</td>
- </tr>
- <tr>
- <td>Address:</td>
- <td>@Html.TextBox("Address")@Html.ValidationMessage("Address", "*")</td>
- </tr>
- <tr>
- <td>Technology:</td>
- <td>@Html.TextBox("Technology")@Html.ValidationMessage("Technology", "*")</td>
- </tr>
- <tr>
- <td>Experience:</td>
- <td>@Html.TextBox("Experience")@Html.ValidationMessage("Experience", "*")</td>
- </tr>
- <tr>
-
- <td>Contact No.: </td>
- <td>@Html.TextBox("ContactNo")@Html.ValidationMessage("ContactNo", "*")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="Submit" /></td>
- </tr>
-
- </table>
- }
-
- </form>
-
- @section Scripts {
- @Scripts.Render("~/Scripts/jquery.validate.js")
- @Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
- @Scripts.Render("~/Scripts/MicrosoftAjax.debug.js")
- @Scripts.Render("~/Scripts/MicrosoftMvcAjax.debug.js")
- @Scripts.Render("~/Scripts/MicrosoftMvcValidation.debug.js")
- }
- Now click on Internet Explorer and run the program.
- The UI will open as below.
- 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.