I have a view that contains many partial views and I need to pass to each one the matching model.
I’ve found 2 ways to do this, but I don’t know whats the real way it should be done.
-
I thought of creating big class that contain all the models as properties and than i can send the models to each partial view. the problem is that its hard typed, and if i need to pass a diffrent combination of models it wont fit.
-
The other way I though of is having a methods in each model that bring me the model for each partial view (GetMenuBar() and so on).
Whats the right way doing that?
My advice, go with Option 1. I use that with all of my Main View/Multiple Partial View scenarios. It’s easy to maintain as each partial has it’s own ViewModel. It keeps the whole thing nice and clean
I use the exact same setup like so:
Each
PartialViewXViewModelis it’s own ViewModel and if need be can be reused in another view.Your Action that renders can look like so:
Your View
Define the UI for each
PartialXlike:Now, each Partial view html and each model that they use can be used anywhere.
The great part is now if you have a page that needs only 2 of these you just create a new
ViewModelto represent that specific view like so:And use it in another view like so:
And that’s perfectly acceptable and also the preferred design strategy in MVC, to have ViewModels that specifically represent what a view needs and only what it needs.
You may want to use a different method for populating your models tho. Most here would recommend using Automapper. Either way, the above just initializes the PartialViewXModels in the constructor of the MainViewModel. That won’t necessarily be your case if you are populating those models with data from your DB. You would want your own strategy for that. This would work here:
This all would just get you started with the design, you can tweak it to your hearts content 🙂