2

我正在开发一个 MVC 3 网站。我在网站上有一个 jquery 菜单。并非所有页面都使用菜单。所以登录页面或注销如果没有授权页面没有菜单。我通过在我的 _Layout 页面中有一个如下所示的 RenderSection 来做到这一点。

 @RenderSection("Menu", required: false)

然后在应该有菜单的页面中,我包含以下部分:

@section Menu{
    <ul id="menu" style="width: 150px; height: 150px; margin-right: 10px; z-index: 9999;">
        <li>@Html.ActionLink("Dashboard", "Dashboard", "Home", null, new { @class = "menu-text" })</li>
        <li>@Html.ActionLink("History", "ViewHistory", "History", null, new { @class = "menu-text" })</li>
        <li><a class="menu-text" href="#">Reports</a>
            <ul style="z-index: 9999;">
                <li>@Html.ActionLink("File Report", "ViewFileReport", "Reports", null, new { @class = "menu-text" })</li>
                <li><a class="menu-text" href="#">New Files</a></li>
                <li><a class="menu-text" href="#">Old Files</a></li>
            </ul>
        </li>
        <li><a class="menu-text" href="#">Admin</a>
            <ul style="z-index: 9999;">
                <li>@Html.ActionLink("Change Password", "ChangePassword", "Home", null, new { @class = "menu-text" })</li>
            </ul>
        </li>
        <li>@Html.ActionLink("Log Off", "LogOff", "Home", null, new { @class = "menu-text" })</li>
    </ul>
}

当我现在添加到菜单并更改内容并添加更多页面时,我意识到在我需要的所有页面上都有这个 RenderSection,但不在我的登录页面或第一个登陆页面上,因为我必须在 8 中编辑它/目前有 9 个地方,而且只会增加。什么是实现我想要的更好的方法?

4

2 回答 2

3

是的,你想把你的菜单标记放在你的 中_Layout.cshtml,但只在需要时才渲染它。

您可以bool在您的值中输入一个值ViewBag以打开需要它的页面中的菜单。

在您的控制器操作中,输入:

ViewBag.ShowMenu = true;

然后_Layout.cshtml你可以输入这样的条件:

@if ( ViewBag.ShowMenu == true )
{
  <ul id="menu" ... your menu markup here
  </ul>
}
于 2013-01-04T10:37:34.923 回答
0

您可以将其放入文件App_Code夹中的文件中,如下所示:

App_Code\Helpers.cshtml

@helper Menu()
{
    <ul id="menu" style="width: 150px; height: 150px; margin-right: 10px; z-index: 9999;">
        <li>@Html.GetPageHelper().ActionLink("Dashboard", "Dashboard", "Home", null, new { @class = "menu-text" })</li>
        <li>@Html.GetPageHelper().ActionLink("History", "ViewHistory", "History", null, new { @class = "menu-text" })</li>
        <li><a class="menu-text" href="#">Reports</a>
            <ul style="z-index: 9999;">
                <li>@Html.GetPageHelper().ActionLink("File Report", "ViewFileReport", "Reports", null, new { @class = "menu-text" })</li>
                <li><a class="menu-text" href="#">New Files</a></li>
                <li><a class="menu-text" href="#">Old Files</a></li>
            </ul>
        </li>
        <li><a class="menu-text" href="#">Admin</a>
            <ul style="z-index: 9999;">
                <li>@Html.GetPageHelper().ActionLink("Change Password", "ChangePassword", "Home", null, new { @class = "menu-text" })</li>
            </ul>
        </li>
        <li>@Html.GetPageHelper().ActionLink("Log Off", "LogOff", "Home", null, new { @class = "menu-text" })</li>
    </ul>
}

并为您的项目添加另一个类,用于从以下代码中GetPageHelper()获取HtmlHelper<T>实例所需的方法App_Code

public static class PageHelper
{
    public static HtmlHelper<object> GetPageHelper(this System.Web.WebPages.Html.HtmlHelper html)
    {
        return ((WebViewPage)WebPageContext.Current.Page).Html;
    }

    public static UrlHelper GetUrlHelper(this System.Web.WebPages.Html.HtmlHelper html)
    {
        return ((WebViewPage) WebPageContext.Current.Page).Url;
    }
}

现在您可以在视图或布局中使用这样的菜单:

@Helpers.Menu()
于 2013-01-04T10:38:20.883 回答