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

↘锁芯ラ 提交于 2019-12-08 01:04:03

问题


I tried to do it but I get an error saying that model x was expected but y was passed in.


回答1:


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"
    })



回答2:


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

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