5
我正在通話中的以下異常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
不爲空,我已驗證了正確的功能。爲什麼MVC認爲這個屬性的空值是父類型的?
我可以通過添加只會呈現ChildView
的代碼來解決此問題,如果ChildProperty
不爲空,但是這一半失敗了觀看點。
我無法解釋的錯誤消息,但這些類型的最佳解決方案的問題是應用**空對象模式**:http://en.wikipedia.org/wiki/Null_Object_pattern –