如何根据mvc中的顶部导航栏选择更改侧边栏菜单?

时间:2015-06-06 22:19:05

标签: asp.net asp.net-mvc twitter-bootstrap

我使用下面的一些引导程序(_layout.cshtml)在mvc中使用顶部导航栏创建了局部视图,但它可以工作,但我想在每个顶部栏导航选择中切换到不同的侧边栏。这该怎么做?在哪里切换侧面菜单?我应该为每个侧面菜单分别进行局部视图吗?如果是,如何在主_layout视图中更改它们? 感谢

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>

    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("SideMenuA", "sm1", "top")</li>
                    <li>@Html.ActionLink("SideMenuB", "sm2", "top")</li>
                    <li>@Html.ActionLink("SideMenuC", "sm3", "top")</li>
                </ul>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <ul class="nav  navbar-fixed-side navbar-fixed-side-left">
            <li>@Html.ActionLink("A", "A", "side")</li>
            <li>@Html.ActionLink("B", "B", "side")</li>
            <li>@Html.ActionLink("C", "C", "side")</li>
        </ul>
    </div>

<div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

</body>
</html>

1 个答案:

答案 0 :(得分:0)

是的,创建单独的侧面菜单部分视图。 并在section ..

中为侧边菜单定义Layout.cshtml
  <div class="col-md-2 column-divider">
      @RenderSection("Sidemenu", required: false)
  </div>

然后,在每个视图中(例如在Index.cshtml中)添加Sidemenu section,以便输出_Sidemenu部分视图..

@{
   ViewBag.Title = "Title";
   Layout = "~/Views/Shared/_Layout.cshtml";
 }

@section Sidemenu
{
   @Html.Partial("_Sidemenu") // side menu partial specific to this view
}

您可以阅读ScottGu's Blog了解详细信息。