2010-02-17 60 views
5

我正在通話中的以下異常Html.RenderPartial錯誤使用強類型的局部視圖時,模型是一個父模型屬性,併爲空

傳遞到字典的模型產品類型'ChildClass',但是這個字典需要一個'ParentClass'類型的模型項。

這兩個類是相關的:

public class ChildClass { /* properties */ } 

public class ParentClass 
{ 
    public ChildClass ChildProperty { get; set; } 

    /* other properties */ 
} 

我有ParentClass一個實例,其中的ChildPropertynull

我有兩個部分視圖,ParentViewViewUserControl<ParentClass>)和ChildViewViewUserControl<ChildClass>)。

在第一個觀點,我有以下...

<% Html.RenderPartial("~/Views/Controls/ChildView.ascx", Model.ChildProperty); %> 

這是扔在這篇文章的頂部列出異常的行。

如果ChildProperty不爲空,我已驗證了正確的功能。爲什麼MVC認爲這個屬性的空值是父類型的?

我可以通過添加只會呈現ChildView的代碼來解決此問題,如果ChildProperty不爲空,但是這一半失敗了觀看點。

+0

我無法解釋的錯誤消息,但這些類型的最佳解決方案的問題是應用**空對象模式**:http://en.wikipedia.org/wiki/Null_Object_pattern –

回答

相關問題