views:

2903

answers:

3

I have a page:

<%@ Page Inherits="System.Web.Mvc.View<DTOSearchResults>" %>

And on it, the following:

<% Html.RenderPartial("TaskList", Model.Tasks); %>

Here is the DTO object:

public class DTOSearchResults
{
 public string SearchTerm { get; set; }
 public IEnumerable<Task> Tasks { get; set; }

and here is the partial:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Task>>" %>

When Model.Tasks is not null, everything works fine. However when its null I get:

The model item passed into the dictionary is of type 'DTOSearchResults' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Task]'.

I figured it must not know which overload to use, so I did this (see below) to be explicit, but I still get the same issue!

<% Html.RenderPartial("TaskList", (object)Model.Tasks, null); %>

I know I can work around this by checking for null, or not even passing null, but thats not the point. Why is this happening?

A: 

I'm having a similar issue maybe the answers given could help you out better than it did me

http://stackoverflow.com/questions/626457/strongly-typed-partial-views-mvc-rc1

Ayo
Yeah I read that before posting but it doesn't seem to help, ill have another read
Andrew Bullock
+43  A: 

Andrew I think the problem you are getting is a result of the RenderPartial method using the calling (view)'s model to the partial view when the model you pass is null.. you can get around this odd behavior by doing:

<% Html.RenderPartial("TaskList", Model.Tasks, new ViewDataDictionary()); %>

Does that help?

meandmycode
brilliant, thanks
Andrew Bullock
Works great, thanks!!!
Jason
This saved me a lot of time!
Mathlec
+1, i'd have wasted hours figuring this out.
richeym
+3  A: 

It appears that when the property of the Model you're passing in is null MVC intentionally reverts back to the "parent" Model. Apparently the MVC engine interprets a null model value as intent to use the previous one.

Slightly more details here: http://stackoverflow.mobi/question1049027_ASP-NET-MVC--strongly-typed-views--partial-view-parameters-glitch-.aspx

Zack