如何在主视图中调用具有模型的局部视图,两个视图都具有不同的模型

问题描述 投票:0回答:1

我有一个主视图,我想在其中调用局部视图,但是我的局部视图不是静态的,它具有模型,并且当我编写@Html.Partial("MyPartialView")时,两个模型(主视图模型和局部视图模型都不同)它表示对象引用未设置为实例,这意味着

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information 
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set 
to an instance of an object.

这意味着我还需要调用控制器方法以及部分视图,如何基本上在部分视图中实现此目的,我正在列出我的记录。

asp.net-mvc partial-views
1个回答
0
投票

您可以在主视图的操作中准备部分视图模型。您应该创建一个具有两个属性的ViewModel,其中一个是主视图的模型,另一个是局部视图的模型。像这样:

© www.soinside.com 2019 - 2024. All rights reserved.