views:

1169

answers:

4

In my main page (call it index.aspx) I call

<%Html.RenderPartial("_PowerSearch", ViewData.Model);%>

Here the viewdata.model != null When I arrive at my partial

<%=ViewData.Model%>

Says viewdata.model == null

What gives?!

+1  A: 

Have you tried just passing in ViewData instead of ViewData.Model? This is an abridged version what I use in my helpers (shamelessly stolen from the Storefront series):

    /// <summary>
    /// Renders a LoggingWeb user control.
    /// </summary>
    /// <param name="helper">Helper to extend.</param>
    /// <param name="control">Type of control.</param>
    /// <param name="data">ViewData to pass in.</param>
    public static void RenderLoggingControl(this System.Web.Mvc.HtmlHelper helper, LoggingControls control, object data)
    {
        string controlName = string.Format("{0}.ascx", control);
        string controlPath = string.Format("~/Controls/{0}", controlName);
        string absControlPath = VirtualPathUtility.ToAbsolute(controlPath);
        if (data == null)
        {
            helper.RenderPartial(absControlPath, helper.ViewContext.ViewData);
        }
        else
        {
            helper.RenderPartial(absControlPath, data, helper.ViewContext.ViewData);
        }
    }

Note that I pass in the current ViewData and not the Model.

Simon Steele
Thanks, that did the trick.Another subtle move in my conversion to the beta1 version.
borisCallens
A: 

How do I go about this then:

I have a (strongly typed) Viewdata (SearchViewData) that has a field Colors which in its turn is a ColorViewData.

So in the Search.aspx I want to render the partial _ColorLIst.ascx that expects a ColorViewData.

I used to do

<%=Html.RenderPartial("_ColorList.ascx", ViewData.Model.Colors%>

You would think in beta1 this would become

<%Html.RenderPartial("_ColorList.ascx", ViewData.Model.Colors;%>

But it gives me the model == null error

borisCallens
A: 

This is untested:

<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(ViewData.Model.Colors));%>

Your control view is expecting view data specific to it in this case. If your control wants a property on the model called Colors then perhaps:

<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>
Simon Steele
A: 

Well, the partial is expecting ColorViewData

public partial class _ColorList : ViewUserControl<ColorViewData>
{
}

And I'm feeding it just that

<%Html.RenderPartial("~/Views/Color/_ColorList.ascx", ViewData.Model.Colors);%>

ViewData.Model.Colors.GetType() == CommonProject.Web.Controllers.ColorViewData

No matter how I try it, allways null

(ViewDataDictionary)ViewData.Model.Colors
(ColorViewData)ViewData.Model.Colors

I don't get the last solution you gave. Should I manually recreate every field that's in the viewdata? I sure hope not ...

borisCallens