Master页面是否可以访问Controller属性?

时间:2010-06-21 14:46:23

标签: c# asp.net-mvc master-pages controller

我需要根据PageController中填充的数据动态加载HTML包装器,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); } } 是所有其他控制器实现的基类。

这是PageController:

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

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

        </asp:ContentPlaceHolder>
    </div>    

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

我目前正在实施我的母版页面,如下所示:

GetWrapperTop()

但这意味着我必须拥有GetWrapperBottom()&amp;所有需要包装器的控制器中定义的<% Html.RenderAction("GetWrapperBottom", "SearchResults"); %> ,我还必须为我想要的每种类型的Wrapper都有一个Master页面。例如,SearchResultsController的Master页面将具有:

<%= this.WrapperTop %>

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

        </asp:ContentPlaceHolder>
    </div>    

<%= this.WrapperBottom %>

理想情况下,它只是

WrapperTop

母版页如何访问WrapperBottom中的PageController和{{1}}值?

1 个答案:

答案 0 :(得分:3)

事实证明,以下是有效的,我从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);
    }
}

然后在主页面中:

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

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

        </asp:ContentPlaceHolder>
    </div>    

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