ASP.Net MVC Scribblings
  • Introduction
  • Introduction
    • My ASP.net MVC Scribblings
    • Test
  • ASP.net MVC
    • Routing
    • Passing Data in MVC
      • ViewData and ViewBag
      • ViewData / ViewBag
      • TempData
      • Session Variables
    • ModelBinder
    • Advantages of MVC over WebForms
    • Data Annotation
    • RAZOR
    • ActionName decorator for overloaded Controller actions
    • Security
      • CSRF
      • XSS
    • Shared Layout page
    • Custom Claims-based Windows Authentication in ASP.net MVC 5
Powered by GitBook
On this page

Was this helpful?

  1. ASP.net MVC

Data Annotation

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace MyAwesomeMVCApp.Models{
  public class Customer
  {
      [Required]
      [RegularExpression("^[A-Z]{3,3}[0-9]{4,4}$")]
      public string CustomerCode { get; set; }

      [Required]
      [StringLength(25)]
      public string CustomerName { get; set; }

  }

}

The [Required], [StringLength] and [RegularExpression] method decorators in the above code sample are the data annotations.

When do these validations happen

As soon as the model object is instantiated from the request or response data, the data annotation validations are run and if any errors are found, the errors are sent to ModelState class.

PreviousAdvantages of MVC over WebFormsNextRAZOR

Last updated 5 years ago

Was this helpful?