ASP.NET MVC Integration

Overview

The ASP.NET MVC Integration functionality of SpecExpress should be considered a Beta release.

The goal of the ASP.NET MVC Integration is to eliminate (as much as possible) the need to write redundant client side rule logic in JavaScript or JQuery. This goal is achieved by the use of a custom Model Validator (SpecExpressModelProvider)that is registered with the ModelValidatorProviders collection at application start. This provider simply looks at the model being validated, and if the model has a specification registered in SpecExpress' ValidationCatalog, it will then generate both the server side validators and client side validators for the model.

Unlike other validation frameworks, out of the box, SpecExpress supports client side validation for quite a vast selection of rules such as:
  • Required
  • MinLength
  • MaxLength
  • LengthBetween
  • LengthEqualTo
  • Alpha
  • Numeric
  • Matches
  • GreaterThan
  • LessThan
  • GreaterThanEqualTo
  • LessThanEqualTo
  • Between
  • EqualTo
It even supports validating, at the client, one property against another such as "Check(c => c.StartDate).Required.LessThan(c => c.EndDate);"!

Although SpecExpress's MVC integration to validate as much as possible at the client, there are certain scenarios that it currently does not support:
  • Any Collection rule such as Contains, ForEach, IsEmpty, etc is not supported since many times the collection being validated is only available at the server.
  • The IsInFuture and IsInPast Date Validators are not supported since the current date at the browser can not be assumed to be correct.
  • Custom rules defined in an "Expect" rule are not supported.
  • Complex boolean logic that include rules combined with "Or" or "Group" relations are not supported.

1. Configure Validation Catalog and Register SpecExpressModelProvider

In the Global.asax.cs:
protected void Application_Start()
{
    // Initialize the Validation Catalog.  This must be done before adding SpecExpress' Model Validator
    // Provider to the ModelValidatorProviders collection.
    ValidationCatalog.Scan(a => a.TheCallingAssembly());

    // Don't imply that value types are required
    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

    // Add the SpecExpressModelProvider to the collection of ModelValidators
    ModelValidatorProviders.Providers.Add(new SpecExpressModelProvider());

    // Register Routes, GlobalFilters, etc.
}

2. Add specexpress.unobtursive.js in Project and Reference it in View

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/specexpress.unobtrusive.js")" type="text/javascript"></script>
That's it! No need to add any attributes to the model, no need to do anything special in the view. If the model has a specification defined in the ValidationCatalog, then the model will bevalidated with it! For server side validation you can use the normal ASP.Net MVC ModelState.IsValid property verify the model is valid and take appropriate action.

Last edited Jun 16, 2011 at 4:19 AM by rbell80134, version 8

Comments

No comments yet.