An error with a strongly typed partial representation when the model is a property of the parent model and is equal to zero

I get the following exception on call Html.RenderPartial:

The model element passed to the dictionary is of the "ChildClass" type, but this dictionary requires a model element of the "ParentClass" type.

These two classes are related to this:

public class ChildClass { /* properties */ }

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

    /* other properties */
}

I have an instance ParentClasswhere the value ChildPropertyis equal null.

I have two partial views, ParentView( ViewUserControl<ParentClass>) and ChildView( ViewUserControl<ChildClass>).

In the first view, I have the following ...

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

This is the line that throws the exception indicated at the top of this message.

, ChildProperty . MVC , ?

, , ChildView, ChildProperty , .

+5
1

: renderpartial

, :

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

All Articles