Wednesday, April 2, 2014

Server side validation in MVC Razor using data annotation

Introduction: In this article I will explain how we can implement the Server side validation in MVC Razor code first approach using data annotation.
Server side validation


Server side validations are required to receive the correct and valid data.

Here I add a class to Model named Employee.cs. Add the namespace to class:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class Employee
        public int Id { get; set; }
        [Display(Name = "Employee Name")]
        [Required(ErrorMessage = "Please Enter Employee name")]
        public string Name { get; set; }
        [Display(Name = "Email Address")]
        [Required(ErrorMessage = "Please Enter Email address")]
        [RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Address")]
        public string Email { get; set; }
        [Display(Name = "Mobile No.")]
        [Required(ErrorMessage = "Please Enter Mobile No")]
        [StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
        public int MobileNo { get; set; }
        [Display(Name = "Address")]
        [Required(ErrorMessage="Please Enter Address")]
        public string Address { get; set; }

Bulid the project and add new Controller (EmployeeController.cs).

Now you see the Create action in EmployeeController.cs:

public ActionResult Create()
            return View();

        // POST: /Employee/Create

        public ActionResult Create(Employee employee)
            if (ModelState.IsValid)
                return RedirectToAction("Index");

            return View(employee);

We use the ModelState.IsValid property to validate the data.
After that check the Create.cshtml in View>> Employee folder.

@using (Html.BeginForm()) {


        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)

        <div class="editor-label">
            @Html.LabelFor(model => model.MobileNo)
        <div class="editor-field">
            @Html.EditorFor(model => model.MobileNo)
            @Html.ValidationMessageFor(model => model.MobileNo)

        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)

            <input type="submit" value="Create" />

    @Html.ActionLink("Back to List", "Index")

@section Scripts {

Build the project and run.

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment