views:

30

answers:

1

I have 2 questions.

First, background:

I have a PageController : Controller and SearchController : PageController

PageController is defined as:

public class PageController : Controller
{
    protected ISite Site;
    protected PageConfiguration PageConfiguration;

    protected override void Initialize(RequestContext rc)
    {
        this.Site = new Site(SessionUser.Instance().SiteId);

        this.PageConfiguration = this.Site.CurrentSite.GetPage(/* controller name */);

        base.Initialize(rc);
    }
}

I have Site & PageConfiguration stored in PageController because every page that implements PageController needs them

Question 1:
I'm trying to port this from an existing ASP.NET app. that requires a page name to get the PageConfiguration object. Instead of a page name, I'm going to use the Controller's name instead. What's the best way for me to get that as a string?

Question 2:
I'm trying to render a PartialView which relies on that data, as follows:

<%@ Page Title="" 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<PageConfiguration>" %>
<%@ Import Namespace="tstFactsheetGenerator.Models.Panels"%>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <% Html.RenderPartial("Search", new Search { PanelConfiguration = Model.Panels[0] }); %>
</asp:Content>

I can see that when the RenderPartial() method is called the PageConfiguration Model has has the Panel I need. But in the constructor for the Search class that PanelConfiguration is null.

Is there a better & more effective way to make the data I need for the page available? Thanks.

A: 

Question 1:

protected override void Initialize(RequestContext rc)
{
    ...
    var controllerName = requestContext.RouteData.Values["controller"]
    ...
}

Question 2:

As you are using class initializer syntax (new { PanelConfiguration = ... }) the PanelConfiguration will be null in the constructor of the Search class but does it matter? You shouldn't be using the PanelConfiguration property there. In the Search partial it will be initialized. If you need to use it in the constructor (I don't see why you would need it) you could add it as constructor argument.

Darin Dimitrov
Thanks Darin. The reason I need an instance of a Search object is becase PanelConfiguration is a bit meaningless on its own in a PartialView. I need to use the info in PanelConfiguration to get the data Search needs to populate the Partial with. Can you tell me if there's a better way to go about this?
DaveDev