Passing null child object from parent object to a partial view
- by Mike
I have an object which contains models for my ASP.NET MVC web app. The Model that is being passed into the view has sub models for "gadgets" on that particular view. Each of these sub models gets passed to a partial view (gadget).
The problem is when I have a null model in the view model. See example below.
View Model:
public class FooBarHolder()
{
public FooBar1 FooBar1 { get; set; }
public FooBar2 FooBar2 { get; set; }
}
We pass FooBarHolder into the view and inside the view we make calls such as
<% Html.RenderPartial("Foo", Model.FooBar1); %>
<% Html.RenderPartial("Foo2", Model.FooBar2); %>
Now say for instance that Model.FooBar2 was null. What I am experiencing from the strongly typed partial view is an error that says "This view expected a model of type FooBar2 but got a model of type FooBarHolder."
Why is this happening instead of just passing in a null?