Some time back I blogged about building my own blog, well I started building it in my spare time. As I mentioned previously, I am using ASP.NET MVC2 and .NET 4 as my platform.
To start with today, I would walk through how to build a simple contact me page by using ASP.NET MVC2's Model Validation Techniques.
First let us put the View Page together -
Below is the content section of my View which takes in a ContactMe as a Model
Here is the Header -
| 1 | <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Berkeley.Models.ContactMe>" %> |
And here is the View Page - Notice the Html.EnableClientValidation() in Line Number about which I will be talking in depth below.
| 1 | <% Html.EnableClientValidation(); %> |
| 2 | <% using (Html.BeginForm()) |
| 3 | {%> |
| 4 | <fieldset> |
| 5 | <legend>Contact Me</legend> |
| 6 | <div class="editor-label"> |
| 7 | <%= Html.LabelFor(model => model.ContactMe.Name) %> |
| 8 | </div> |
| 9 | <div class="editor-field"> |
| 10 | <%= Html.TextBoxFor(model => model.ContactMe.Name) %> |
| 11 | <%= Html.ValidationMessageFor(model => model.ContactMe.Name) %> |
| 12 | </div> |
| 13 | <div class="editor-label"> |
| 14 | <%= Html.LabelFor(model => model.ContactMe.Email) %> |
| 15 | </div> |
| 16 | <div class="editor-field"> |
| 17 | <%= Html.TextBoxFor(model => model.ContactMe.Email) %> |
| 18 | <%= Html.ValidationMessageFor(model => model.ContactMe.Email) %> |
| 19 | </div> |
| 20 | <div class="editor-label"> |
| 21 | <%= Html.LabelFor(model => model.ContactMe.Subject) %> |
| 22 | </div> |
| 23 | <div class="editor-field"> |
| 24 | <%= Html.TextBoxFor(model => model.ContactMe.Subject) %> |
| 25 | <%= Html.ValidationMessageFor(model => model.ContactMe.Subject) %> |
| 26 | </div> |
| 27 | <div class="editor-label"> |
| 28 | <%= Html.LabelFor(model => model.ContactMe.Message) %> |
| 29 | </div> |
| 30 | <div class="editor-field"> |
| 31 | <%= Html.TextAreaFor(model => model.ContactMe.Message) %> |
| 32 | <%= Html.ValidationMessageFor(model => model.ContactMe.Message) %> |
| 33 | </div> |
| 34 | <p> |
| 35 | <input type="submit" name="btnSendMessage" value="Send Message" class="stylizedBtn" /> |
| 36 | </p> |
| 37 | </fieldset> |
| 38 | <% } %> |
Notice the New TextBoxFor , TextAreaFor , ValidationMessageFor - Html Helper methods which are introduced in ASP.NET MVC2 and can take in LINQ Lamda Syntax(which I Love :), I will write a follow up post soon on these methods soon).
So the view is pretty straight forward which takes in a Model and Displays a simple Insert Form.
To get Client Side Validation in ASP.NET MVC2 to work all we have to do is drop in references to MicrosoftMVCValidation.js and MicrosoftAjax.js (which ship with the default project template for MVC2) and call Html.EnableClientValidation(). That's it from a View Point of view.
And from a Model point of view here's how we define validation rules. For this example I am going to be using Entity Framework, so the ORM generates my classes for me. I am going to use something called "buddy classes". Here a more about using buddy classes in Detail.
Using Data Annotations with Entity Framework Classes -
After letting EF generate the classes, we write a partial class for the generated class, here is a snippet of how the partial class would look like -
| 1 | namespace Berkeley.Models |
| 2 | { |
| 3 | [MetadataType(typeof(ContactMeValidator))] |
| 4 | public partial class ContactMe |
| 5 | { |
| 6 | |
| 7 | } |
| 8 | |
| 9 | } |
Notice above, ContactMe is the class generated by Entity Framework and ContactMeValidator is our buddy class which holds the validation rules. All we are saying above is letting the framework know that if it encounters a Class named ContactMe then its MetaData information is in a Class Named ContactMeValidator.
Here's what a buddy class with validation logic would look like -
| 1 | namespace Berkeley.Models |
| 2 | { |
| 3 | [Bind(Exclude="ContactMeID")] |
| 4 | public class ContactMeValidator |
| 5 | { |
| 6 | [Required(ErrorMessage="Name is Required")] |
| 7 | [StringLength(50,ErrorMessage="Name Must be Under 50 Characters")] |
| 8 | public String Name { get; set; } |
| 9 | |
| 10 | [Required(ErrorMessage = "Email is Required")] |
| 11 | [StringLength(50, ErrorMessage = "Email Must be Under 200 Characters")] |
| 12 | [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", ErrorMessage = "Email Not in Valid Format")] |
| 13 | public String Email { get; set; } |
| 14 | |
| 15 | [Required(ErrorMessage = "Subject is Required")] |
| 16 | [StringLength(50, ErrorMessage = "Subject Must be Under 500 Characters")] |
| 17 | public String Subject { get; set; } |
| 18 | |
| 19 | [Required(ErrorMessage = "Message is Required")] |
| 20 | public String Message { get; set; } |
| 21 | |
| 22 | } |
| 23 | } |
Note : I am using .NET 3.5's Data Annotations. with
Required,StringLength, and RegularExpressionkeywords.
That is essentially it. We defined the validation rules in a buddy class which extends to hold the meta data information of classes generated by Entity Framework.
Here is a screen shot of how the webpage would look like

I will upload a code sample of this example soon.
Hope this helps,
Cheers,
Venkata