在调用Html.RenderPartial
时,我遇到了以下异常:
传递到字典中的模型项类型为“ChildClass”,但此字典需要一个类型为“ParentClass”的模型项。
这两个类是相关的:
public class ChildClass { /* properties */ }
public class ParentClass
{
public ChildClass ChildProperty { get; set; }
/* other properties */
}
我有一个ParentClass
实例,其中ChildProperty
的值为null
。
我有两个部分视图,ParentView
(ViewUserControl<ParentClass>
)和ChildView
(ViewUserControl<ChildClass>
)。
在第一个视图中,我有以下内容...
<% Html.RenderPartial("~/Views/Controls/ChildView.ascx", Model.ChildProperty); %>
这是导致本帖顶部列出的异常的那一行。
如果ChildProperty
不为null,我已经验证了正确的功能。为什么MVC认为此属性的null值是父类型?
如果ChildProperty
为null,则可以通过添加仅在ChildProperty
不为null时呈现ChildView
的代码来解决此问题,但这会部分破坏视图的目的。