Scott Guthrie on MVC 2 Model Validation

Tags: ASP.NET MVC

Scott Guthrie in his latest blog  post explains one of the cool new features in ASP.NET MVC 2: Model Validation. MVC already had a way of validating that worked pretty well, but with MVC 2 has improved on this with new DataAnnotation Validation attributes.

using System;
using System.ComponentModel.DataAnnotations;

namespace fekkedotcomMVC.Models
{
    public class Person
    {        
        //Example of required attributes.        
        [Required(ErrorMessage="First Name is required")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name is required")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Email is required")]
        public string Email { get; set; }
    }
}

There are also attributes for length and regular expressions. You can also add your own by extending one of the annotation classes.

Add a Comment

Share This Page

Weather

Observation Location: KJAX
icon
Clear
Temperature: 43 F (6 C)
Relative Humidity: 80%
Wind: From the SW at 5 MPH
Wind Dir: SW
Wind Degrees: 230
Wind Mph: 5
Wind Gust Mph:
Pressure: 30.20 in (1023 mb)
Dewpoint: 37 F (3 C)
Windchill: 40 F (5 C)
Visibility: 10.0
Forecast
History
Location Jacksonville, Florida

My Twitter