views:

34

answers:

1

I need to load a HTML wrapper dynamically depending on data that gets populated in the PageController, which is a base class that all other controllers implement.

This is the PageController:

public class PageController : Controller
{
    protected PageConfiguration PageConfiguration;
    public string WrapperTop { get; set; }
    public string WrapperBottom { get; set;}

    protected override void Initialize(RequestContext rc)
    {

        // the PageConfiguration is determined by the 
        // Controller that is being called
        var pageName = rc.RouteData.Values.Values.FirstOrDefault();
        this.PageConfiguration = GetPageConfiguration(pageName.ToString());

        WrapperManager wm = GetWrapperManager(this.PageConfiguration.Id);
        this.WrapperTop = wm.WrapperPartOne;
        this.WrapperBottom = wm.WrapperPartTwo;

        base.Initialize(rc);
    }
}

I'm currently implementing my Master page as follows:

<% Html.RenderAction( "GetWrapperTop", "FundFactsheet"); %>

    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">

        </asp:ContentPlaceHolder>
    </div>    

<% Html.RenderAction("GetWrapperBottom", "FundFactsheet"); %>

But this means that I have to have GetWrapperTop() & GetWrapperBottom() defined in all Controllers that need a wrapper, and I also have to have a Master page for every type of Wrapper that I want. For instance, the Master page for SearchResultsController would have:

<% Html.RenderAction("GetWrapperBottom", "SearchResults"); %>

Ideally it'd just be

<%= this.WrapperTop %>

    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">

        </asp:ContentPlaceHolder>
    </div>    

<%= this.WrapperBottom %>

How can the Master page access the WrapperTop and WrapperBottom values in PageController?

+1  A: 

It turns out the following worked, which I figured out from here

public class PageController : Controller
{
    protected PageConfiguration PageConfiguration;
    public string WrapperTop { get; set; }
    public string WrapperBottom { get; set;}

    protected override void Initialize(RequestContext rc)
    {

        // the PageConfiguration is determined by the 
        // Controller that is being called
        var pageName = rc.RouteData.Values.Values.FirstOrDefault();
        this.PageConfiguration = GetPageConfiguration(pageName.ToString());

        WrapperManager wm = GetWrapperManager(this.PageConfiguration.Id);
        ViewData["WrapperTop"] = wm.WrapperPartOne;
        ViewData["WrapperBottom"] = wm.WrapperPartTwo;

        base.Initialize(rc);
    }
}

And then in the Master page:

<%= (string)ViewData["WrapperTop"] %>

    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">

        </asp:ContentPlaceHolder>
    </div>    

<%= (string)ViewData["WrapperBottom"] %>
DaveDev