I have what I think is a somewhat normal situation where I need to bind form records to the "order" model. This model has several levels of information:
Order.Billing.FirstName Order.Billing.Address.City Order.Billing.Address.Country
Using DefaultModelBinder, if I POST the form for an action that takes this order model as a parameter, the following JustWork (TM) fields:
<%=Html.TextBox("Billing.FirstName")%> <%=Html.TextBox("Billing.Address.City")%>
In this field there is no:
<%=Html.TextBox("Billing.Address.Country")%>
I have wrinkles with the country. In our case, Address.Country returns an instance of the Country class (ISO2 / 3 / name / code logic). This is not a string. Not surprisingly, it does not work by default.
My first thought was to create a CountryModelBinder (inherit DefaultModelBinder) and ModelBinders.Binders. Add it to the country type. When I do this, CountryModelBinder will never be called in the scenerio above.
My second thought was to create an AddressModelBinder (inherit DefaultModelBinder) and bind it to our address type. Although the call is called, the call to SetProperty for "Country" has an empty value, although the field "Billing.Address.Country" is posted on the form.
After some intervention, it seems that the model binding behavior calls CreateModel calls when the model is the top-level class that the action requires and all other binders have their own BindPropery / SetProperty called child properties.
In other words, if I create model bindings for Order, OrderAddress (Billing), Address, and Country. For an action that takes an order, only OrderModelBinder.CreateModel is called. ORDERAddress and Address.BindProperty / SetProperty are called for some things, and sometimes the argument of the value of SetProperty is empty when it is explicitly placed in a name that matches other field property mappings.
Just add code to OrderModelBinder to pull Billing.Address.Country from Request.Form. But I have several models that use Address, and they all look broken.
What am I missing here? Is there a way to get CountryModelBinder to actually get the call in this case? I would think that CountryModelBinder should be called when Billing.Address.Country is mapped to the Country property of the binding address.