In this blog, I’m explaining how to do model validation in asp.net mvc 4.
Step 1:
Open Visual Studio 2012 and create an asp.net mvc 4 application and add a controller named “HomeController.cs” to the controller folder in the solution explorer.
using ModelValidationApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ModelValidationApp.Controllers
{
publicclassHomeController : Controller
{
//
// GET: /Home/
[HttpGet]
publicViewResult Index()
{
return View();
}
}
}
Step 2:
Next add a model class named “Student.cs” like below:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ModelValidationApp.Models
{
publicclassStudent
{
[Required(ErrorMessage="Name is required")]//Make the field required
publicstring name { get; set; }
[Required(ErrorMessage="Address is required")]
publicstring address { get; set; }
[Required(ErrorMessage="Email is required")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Invalid Email")]//Check for valid email address
publicstring email { get; set; }
}
}
In an MVC application, validation is typically applied in the domain model, rather than in the user interface. This means that we define our validation criteria in one place, and it takes effect in any place the model class is used. ASP.NET MVC supports declarative validation rules defined with attributes from the System.ComponentModel.DataAnnotations namespace.
Step 3:
Now add a view to the project by right clicking on the “Index()” method in HomeController.cs and named it “Index.cshtml” and make it a strongly-typed view like below:
@model ModelValidationApp.Models.Student
@{
Layout = null;
}
<!DOCTYPEhtml>
<html>
<head>
<metaname="viewport"content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
<h2>Registration Form</h2><br/>
@using(Html.BeginForm())
{
<p>Name : @Html.TextBoxFor(m=>m.name) @Html.ValidationMessageFor(m=>m.name)</p>
<p>Email : @Html.TextBoxFor(m=>m.email) @Html.ValidationMessageFor(m=>m.email)</p>
<p>Address : @Html.TextBoxFor(m=>m.address) @Html.ValidationMessageFor(m=>m.address)</p>
<inputtype="submit"value="Submit"/>
}
</div>
</body>
</html>
@Html.ValidationMessageFor() – will display the error message according to the field.
Step 4:
Now add another viewresult to the HomeController.cs like this:
publicViewResult Index(Student student)
{
if (ModelState.IsValid)
{
return View("Thanks",student);
}
else
{
return View();
}
}
If ModelState will be valid then it will display the next view otherwise it will show the same view with validation error messages.
Step 5:
Now add another view to the project to display the values entered by user. To add a view, right click the newly created index() viewresult method in the HomeController and named it “Thanks.cshtml” and make it a strongly – typed view like below:
@model ModelValidationApp.Models.Student
@{
Layout = null;
}
<!DOCTYPEhtml>
<html>
<head>
<metaname="viewport"content="width=device-width"/>
<title>Thanks</title>
</head>
<body>
<div>
<h2>Thank you, @Model.name</h2>
</div>
</body>
</html>
Output
When you run the application.
If you click the submit button:-
Validation error messages will be displayed like above:
Fill the form with the appropriate values and click on submit button.
Leave Comment