0

What should I do, when I need one ViewModel class to be passed to the asp-page (containing various data to display) and another Model class I need to be submitted (through the form tag) back to the server?

tereško
  • 58,060
  • 25
  • 98
  • 150
AgentFire
  • 8,944
  • 8
  • 43
  • 90

3 Answers3

1

Nest the "Other View Model" inside the main View Model. Then bind the controls of the form to the sub-model's properties.

Action Method Signature:

[HttpPost]
public ActionResult Foo(SubModel model) { /* ... */ }
Alex
  • 34,899
  • 5
  • 77
  • 90
1

You can use a Custom Model Binder to bind the Input Model based on the form generated by output model. Check this link for more information:

http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/

ASP.NET MVC 3 Model Binding Resources

Community
  • 1
  • 1
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

If the properties of the two view models are more or less same then the default model binder will take care else you have to go for a custom model binder.

The other options if you have to create the html fields with the names of the ViewModel properties that you are going to bind on post.

VJAI
  • 32,167
  • 23
  • 102
  • 164