I mentioned the release of ASP.NET MVC Preview 5, but if you want a real tutorial on all the new features, check out ScottGu's post which got me so excited I was up until 3am last night converting a production app over to some of the new MVC Preview 5 Features.
As Scott so elequently describes them, Model Binders provide a way for complex types to be de-serialized from the incoming HTTP input, and passed to a Controller action method as arguments. This allows us to have a Controller Action such as:
public ActionResult Save(Customer customer)
and have the ASP.NET MVC Framework create an instance of Customer and take the form values in the FormCollection and automatically add them to the properties of Customer.
You can create your own Custom Model Binders for Customer that instantiates a new Customer Instance and adds the appropriate form values to the various Customer Properties, or there is a ComplexModelBinder in ASP.NET MVC Futures that uses a bit of reflection to do the same thing. You can register the ComplexModelBinder in your Gobal.asax Class as follows:
ModelBinders.DefaultBinder = new ComplexModelBinder();
The main thing to remember when using the ComplexModelBinder is the naming convention of your controls in the form. ComplexModelBinder is expecting the Class Name + “.“ to be appended to the property name. Those familiar with the DataBindAttribute in Castle MonoRail will see the similiarities. The DataBindAttribute in MvcContrib may do the same thing.
<%= Html.TextBox(“Customer.Name“) %>
<%= Html.TextBox("Customer.Name") %>
If you are using ComplexModelBinder and not prefixing the Class Name to the name of the Property, you will just get null values for your properties. Although I won't show it here, you can check out the source code of ComplexModelBinder to verify this behavior.
Hope this helps,
Dave