I tried to do it but I get an error saying that model x was expected but y was passed in.
Yes. In fact you can use any class, but it has to match the @model
declaration of your partial view.
Partial View:
@model partialViewModel
<h2>@Model.partialViewModelProperty</h2>
Main View:
@model mainViewModel
<h1>Model.mainViewModelProperty</h1>
@Html.Partial("_PartialView", new partialViewModel()
{
partialViewModelProperty = "A title"
})
No, that is the point of a strongly-typed View. It requires a certain type. A partial view would handle this the same as any other view.
来源:https://stackoverflow.com/questions/9418925/is-it-possible-to-pass-a-partial-view-a-different-model-than-the-model-used-by-t