Is it possible to pass a partial view a different model than the model used by the view it is in?

做~自己de王妃 提交于 2019-12-06 09:22:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!