How do I pass variables to multiple partial views within a view? My idea is that every partial view will make up a different field in a form, and I plan on using dynamically created DOM objects with each partial view. So I need to pass in 3 parameters. An ID (to uniquely identify each DOM object), and its particular controller and action. How would I go about doing this in a view?
For example, I want to do something along the lines of…
@Html.Partial("_BookField", Model, 1, "book", "updateauthor")
@Html.Partial("_BookField", Model, 2, "book", "updatetitle")
@Html.Partial("_BookField", Model, 3, "book", "updatepublisher")
Is there a way to do this without adding additional attributes in the model?
Edit: After conferring with one of my coworkers, he also brought up a point that I’d like clarification on. He suggested that what I’m doing, which is introducing controller/action logic in the view is breaking the MVC concept. But things like ActionLinks, you have to put in the controller and action in an actionlink for it to be able to do something. Is what I’m trying to do considered breaking the MVC paradigm?
You use view models of course:
and then have your main view model different properties of this SubViewModel:
and then simply:
Of course it is the now the controller action responsibility to fill those view models (as always). And if you use editor/display templates your code might be even simpler, just like this:
and because this strangely looks to me like a collection, well, you could use a collection:
and then simply: