Not Equal C && !=C && 不等于西
RSS icon Email icon Home icon
  • Client Side Validation For DataType Attribute in ASP.NET MVC3

    Posted on November 20th, 2012 Sean Add comments

         After Microsoft announced ADO.NET Entity Framework 4.1, database design becomes to very easy via Code First approach. And the DataType Attribute from “System.ComponentModel.DataAnnotations” namespace has been used very popular during Code First. Here are some examples:

         But be noticed, this validation of attribute only fires when you check “ModelState.IsValid” property in Controller or calling “SaveChanges()” in Entity Context, not in the client side. fortunately, JQuery has all the client side validation for those data types. So, the lazy way is just connect them together.

         First we need to use “DataAnnotationsModelValidator” class from “System.Web.Mvc” namespace:

         Then, we need to register this validation to JQuery in “Global.asax.cs”, under function “Application_Start()”:

         Rebuild your MVC3 project, and now you have client side validation when you using “HtmlHelperFor” for your Model properties in Razor!

    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
    Loading...Loading...
    626 views

    Leave a Reply

    Be the First to Comment!