Click here to Skip to main content
13,140,591 members (48,067 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 21 Feb 2011

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

, 22 Feb 2011
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
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

9 members

You may also be interested in...


Comments and Discussions

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