Можно ли для главной страницы получить доступ к свойствам контроллера? - PullRequest
0 голосов
/ 21 июня 2010

Мне нужно динамически загружать оболочку HTML в зависимости от данных, которые заполняются в PageController, который является базовым классом, который реализуют все другие контроллеры.

Это 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);
    }
}

В настоящее время я реализую свою главную страницу следующим образом:

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

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

        </asp:ContentPlaceHolder>
    </div>    

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

Но это означает, что мне нужно определить GetWrapperTop() & GetWrapperBottom() во всех контроллерах, которым требуется оболочка, и мне также нужно иметь главную страницу для каждого типа оболочек, который я хочу. Например, главная страница для SearchResultsController будет иметь:

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

В идеале это было бы просто

<%= this.WrapperTop %>

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

        </asp:ContentPlaceHolder>
    </div>    

<%= this.WrapperBottom %>

Как главная страница может получить доступ к значениям WrapperTop и WrapperBottom в PageController?

1 Ответ

3 голосов
/ 21 июня 2010

Получается следующее сработало, которое я понял из здесь

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);
    }
}

А затем на главной странице:

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

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

        </asp:ContentPlaceHolder>
    </div>    

<%= (string)ViewData["WrapperBottom"] %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...