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?
Asked
Active
Viewed 896 times
0
3 Answers
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/
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