ASP.NET MVC – Castle Binders from MvcContrib

Unfortunately, there is no wiki documentation, etc… on the Castle Binders in the MvcContrib.  So I thought I’d hop in and write up a sample for anyone else interested in using these binders:

First, for some background information from the Castle website : http://www.castleproject.org/monorail/documentation/trunk/usersguide/smartcontroller.html

These binders were originally created for Monorail, and outside of a few items, most of that documentation applies to using with MvcContrib.

I’m using the latest MvcContrib from the trunk (SVN) at :

http://mvccontrib.googlecode.com/svn/trunk

After building the MvcContrib, there assemblies referenced are from:

trunk\src\MvcContrib.Castle\bin\Release (I just added them all to a new Mvc project)

(I’ll include the source here if you just want to download and try it)

The goal of the binders is to provide the ability to include a strongly typed object as the recipient to a form post to an action.  As described in the document above, the default would be:

ie.

Person person = new Person();
person.FirstName = Request.Form[“FirstName”];

Step One: Create Controller implementing the ICastleBindingContainer interface

image

Step Two : Create Actions to handle the Form post

image

(note: the HttpVerbs allow you to specify one named Action – for different uses: the Get is for the form load, the Post is for the form post)

The post action of BindersView takes a ‘Person’ object.   We use the ‘CastleBind’ attribute to specify the binding.

There are a few optional parameters with the CastleBind: The prefix to use, and the Exclude option. 

image

This allows you to specify a prefix of any name in the form itself (I show form next).  The exclude allows for properties to not be included in the post request.

(For example, if you have a sensitive property of Person, ie. social security – you can exclude it from the binding)

Step Three : The Model

image 

Step Four : The Form

Let’s show the form now:

image

The first form fields represent the ‘Person’, and I’m also including form fields for the spouse to be added.

Note the ‘person.FirstName’ – the prefix is required.  Also, the Spouse object is referenced as ‘person.Spouse.FirstName’.  This will help instruct how to bind to the object.

(You can read more about this in the Castle documention  link above)

Let’s run it:

image

On save:

image

As you can see – it has bound to the person object as expected.

I hope this helps show, in a simple example, how to use the Castle Binders in MvcContrib.

(source here)

Advertisements

10 thoughts on “ASP.NET MVC – Castle Binders from MvcContrib

  1. Hi Steve

    Thanks for the post – I’ve linked to it from the mvccontrib wiki.

    One thing to point out, implementing ICastleBindingContainer is completely optional (it is only necessary if you want to supply your own IDataBinder instance). If you do not implement this interface, the standard Castle DataBinder will be used regardless.

    Jeremy

  2. Perhaps someone can help me – a bit of a stumbling block here:

    I’ve noticed when using jQuery to reference a name, it does not like the ‘.’

    ie. $(“#person.FirstName”) – causes some issues.

    (I’d like to see the need for the prefix to be removed, or if defined in the attribute to apply the prefix only on the post)

  3. Hi Steve,

    If I remember well you can just replace the dot by an underscore: $(”#person_FirstName”)

    This is possible because the generated html will look like this:

    (this applies to Moorail, no idea if it it works for MS MVC)

  4. Tried the example code on MVC 2, and recieved the following exception:

    Method ‘BindModel’ in type ‘MvcContrib.Castle.CastleBindAttribute’ from assembly ‘MvcContrib.Castle, Version=0.0.1.0, Culture=neutral, PublicKeyToken=null’ does not have an implementation

    Any ideas?
    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s