Click here to Skip to main content
12,550,397 members (46,386 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Basic Data Validation Using Data Annotations: ASP.NET MVC 3

, 22 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A basic introduction on how to use data annotations for validating MVC models, which solves basic validation issues


Using data annotations for data validation in ASP.NET MVC 3.0


Gives you a basic idea of how to use Data Annotations for validating MVC models which solves basic validation issues like required field, max length, regex and hidden input field.

Using the Code

Create the model first and decorate the properties with the attributes shown below, these come under the namespace System.ComponentModel.DataAnnotations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Mvc3GenericValidation.Models
  #region User Model
    public class User
        [StringLength(6, MinimumLength = 4)]
        [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        //making id as Hidden filed
        [HiddenInput(DisplayValue = false)]
        public int Id { get; set; }


Create the strongly typed view using Razor view engine included in ASP.NET MVC 3.0

@model Mvc3GenericValidation.Models.User
    ViewBag.Title = "CreateUser";
    Layout = "~/Views/Shared/_Layout.cshtml";
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
            <input type="submit" value="Create" />
    @Html.ActionLink("Back to List", "Index")

Points of Interest

The key to note here is the referenced jquery files for unobtrusive validation, i.e. rendering of HTML5 attributes with the elements by the view engine. More information can be gathered from here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
Computer geek very much interested in learning and adopting latest and cutting edge technologies .

Expertise in,c#,WCF,web services,Jquery,MVC ,TTD's and design principles and patterns applying SOLID

Designation - Architect
Group type: Organisation

9 members

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 22 Feb 2011
Article Copyright 2011 by imsauravroy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid