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 :

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 :

After building the MvcContrib, there assemblies referenced are from:

trunksrcMvcContrib.CastlebinRelease (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:


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

Step One: Create Controller implementing the ICastleBindingContainer interface


Step Two : Create Actions to handle the Form post


(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. 


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


Step Four : The Form

Let’s show the form now:


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:


On save:


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)


15 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.


  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=, Culture=neutral, PublicKeyToken=null’ does not have an implementation

    Any ideas?

    1. Unfortunately no – webhost4life screwed up my account so badly all I was able to donwas restore a db copy

      I actually wouldn’t really recommend these at this time I think what comes
      With mvc today would handle this

      1. Thanks for the prompt reply. The challenge that I am facing is trying to use the default model binding to reconstitute a nested object. For example, a Student belongs to a School. In the form, the the list of schools is a dropdownlist. I have not been successful at having the binding work such that Student.School is properly set when the form is posted back.

        Any tips would be greatly appreciated.

      2. I recommend in case where the default model
        Binder doesn’t do what you need to create your own custom model binder. If it’s something trivial you can also always just grab the form collection and pull out you value by key from the request object.

        Try looking up custom model binders and you should find some samples out there rather quickly that should help.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s