Passing null child object from parent object to a partial view

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-17T18:06:40Z Indexed on 2010/03/17 18:11 UTC
Read the original article Hit count: 184

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc