ASP MVC2 model binding issue on POST

Posted by Brandon Linton on Stack Overflow See other posts from Stack Overflow or by Brandon Linton
Published on 2010-04-09T21:07:57Z Indexed on 2010/04/09 21:23 UTC
Read the original article Hit count: 695

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!

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-mvc-2-validation