Click here to Skip to main content
11,709,955 members (87,159 online)
Click here to Skip to main content
Articles » Web Development » Ajax » Samples » Downloads
Add your own
alternative version

Data Validation Using Annotations for jQuery AJAX Calls in MVC Applications

, 4 Jul 2011 CPOL 51.7K 1.5K 33
This article presents an example of how to use data annotations to validate data received from jQuery AJAX calls and how to send validation result as well as HTML content from a partial view to the client in a JSON object in MVC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace DataAnnotationAjax.Models
    public class Student
        public int Id { get; set; }

        [Required(ErrorMessage = "Last name is required")]
        public string LastName { get; set; }
        [Required(ErrorMessage = "First name is required")]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "Score is required to add the student"),
            Range(60, 100, ErrorMessage="Score should be between 60 - 100")]
        public int Score { get; set; }

        public DateTime Enrollment { get; set; }

    public static class StudentRepository
        private static int IdSeed = 1;
        private static readonly List<Student> Students = new List<Student>();

        // Initiate a student list in the static constructor
        static StudentRepository()
            Random rand = new Random();
            for (int i = 0; i < 3; i++)
                var student = new Student();
                int id = IdSeed++;
                student.Id = id;
                student.LastName = "Last Name " + id.ToString();
                student.FirstName = "First Name " + id.ToString();
                student.Score = (60 + Convert.ToInt16(rand.NextDouble() * 40));
                student.Enrollment = DateTime.Now;

        // Add a single student
        public static void AddStudent(Student student)
            student.Id = IdSeed++;
            student.Enrollment = DateTime.Now;

        // Get the list of students
        public static List<Student> GetStudent()
            return Students;


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Dr. Song Li
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Jul 2011
Article Copyright 2011 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid