ASP MVC2 model binding issue on POST
- by Brandon Linton
So I'm looking at moving from MVC 1.0 to MVC 2.0 RTM. One of the conventions I'd like to start following is using the strongly-typed HTML helpers for generating controls like text boxes.
However, it looks like it won't be an easy jump. I tried migrating my first form, replacing lines like this:
<%= Html.TextBox("FirstName", Model.Data.FirstName, new {maxlength = 30}) %>
...for lines like this:
<%= Html.TextBoxFor(x => x.Data.FirstName, new {maxlength = 30}) %>
Previously, this would map into its appropriate view model on a POST, using the following method signature:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Registration(AccountViewInfo viewInfo)
Instead, it currently gets an empty object back. I believe the disconnect is in the fact that we pass the view model into a larger aggregate object that has some page metadata and other fun stuff along with it (hence x.Data.FirstName instead of x.FirstName).
So my question is: what is the best way to use the strongly-typed helpers while still allowing the MVC framework to appropriately cast the form collection to my view-model as it does in the original line? Is there any way to do it without changing the aggregate type we pass to the view?
Thanks!