Reputation: 4692
Can a Partial View and Parent View have two different View Models?
For instance, a partial view may have a dropdownlist specific to that model and a parent view may have another model for some other entities.
Upvotes: 1
Views: 1099
Reputation: 5750
Yes, the partial and parent views can have two different view models.
The partial view has to get it's viewModel from somewhere though, so this could either be just creating it straight from the view
Parent View
@{
var partialViewModel = new SomeOtherModel()
{
PartialName = Model.Name,
SomeProperty = SomeOtherVariable
};
Html.Partial("_MyPartialView", partialViewModel);
}
Or a more common method is the Partial ViewModel is a property of the parent ViewModel
@Html.Partial("_MyPartialView", Model.PartialViewModel)
There are other ways too, but these are fairly common ones you see.
Upvotes: 4
Reputation: 1984
If you call Partial view from the Parent view, the viewModel will be in a way subset of the ViewModel of the parent. You can have different ViewModels for Parent and Partial views.
Upvotes: 1