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