ASP MVC: Submitting a form with nested user controls

Posted by Nigel on Stack Overflow See other posts from Stack Overflow or by Nigel
Published on 2010-04-11T13:33:28Z Indexed on 2010/04/11 13:53 UTC
Read the original article Hit count: 454

Filed under:
|

I'm fairly new to ASP MVC so go easy :).

I have a form that contains a number of user controls (partial views, as in System.Web.Mvc.ViewUserControl), each with their own view models, and some of those user controls have nested user controls within them. I intended to reuse these user controls so I built up the form using a hierarchy in this way and pass the form a parent view model that contains all the user controls' view models within it.

For example:

Parent Page (with form and ParentViewModel)
  -->ChildControl1 (uses ViewModel1 which is passed from ParentViewModel.ViewModel1 property)
  -->ChildControl2 (uses ViewModel2 which is passed from ParentViewModel.ViewModel2 property)
    -->ChildControl3 (uses ViewModel3 which is passed from ViewModel2.ViewModel3 property)

I hope this makes sense...

My question is how do I retrieve the view data when the form is submitted? It seems the view data cannot bind to the ParentViewModel:

public string Save(ParentViewModel viewData)...

as viewData.ViewModel1 and viewData.ViewModel2 are always null. Is there a way I can perform a custom binding?

Ultimately I need the form to be able to cope with a dynamic number of user controls and perform an asynchronous submission without postback. I'll cross those bridges when I come to them but I mention it now so any answer won't preclude this functionality.

Many thanks.

© Stack Overflow or respective owner

Related posts about asp

Related posts about asp.net-mvc