如何在我的MVC4应用程序的主页上禁止导航?

时间:2013-05-15 21:39:57

标签: asp.net-mvc-4

我在VS 2012中使用标准的MVC4模板。它带有一个_layout.cshtml文件,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - iLoveSport</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Styles.Render("~/Content/kendo")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/kendo")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
               <img src="~/Images/logo.png" alt="ILoveSport Logo" />
            </div>
            <div class="float-right">
                <section id="login">

                </section>
                <nav>
                    <ul id="menu">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("AFL", "Index", "AFL")</li>
                        <li>@Html.ActionLink("NRL", "Index", "NRL")</li>
                        <li>@Html.ActionLink("State of Origin", "Index", "State of Origin")</li>
                        <li>@Html.ActionLink("Cricket", "Index", "Cricket")</li>
                        <li>@Html.ActionLink("Golf", "Index", "Gof")</li>
                        <li>@Html.ActionLink("Motorsport", "index", "Motorsport")</li>

                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">
        @RenderSection("featured", required: false)
        <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">

            </div>
        </div>
    </footer>

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

_viewstart.cshtml包含以下内容:

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

必须修改哪些内容才能仅在主页上抑制_layout.cshtml页面中的导航?主页需要一个按钮,然后触发当前我的主页的内页。我是否创建了一个新的布局文件来抑制菜单并更改viewstart来加载它?或者,这可以通过其他方式完成吗?

感谢您的帮助和指导。

更新:
我的家庭控制器现在如下:

[ChildActionOnly]
    public ActionResult NavigationMenu()
    {
        string controller = ControllerContext.
            ParentActionViewContext.RouteData.Values["controller"].ToString();
        string action = ControllerContext.
            ParentActionViewContext.RouteData.Values["action"].ToString();

        if (controller == "Home" && action == "Index")
            return Content("");
        else
            return PartialView("_Menu");
    }

我的_layout.cshtml如下:

<nav>
@Html.Action("NavigationMenu","Partial")
</nav>

但是,我收到一条调试错误,说明:

System.Web.HttpException: The controller for path '/' was not found or does not implement IController.

layout.cshtml文件中抛出此错误。该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以将导航菜单定义为部分视图。并在您的布局中渲染此局部视图。

此局部视图的内部操作方法,您可以检查控制器和操作。如果是您的主页,您可以返回空白内容。否则,请返回导航菜单。

部分视图

<nav>
    <ul id="menu">
        <li>@Html.ActionLink("Home", "Index", "Home")</li>
        <li>@Html.ActionLink("AFL", "Index", "AFL")</li>
        <li>@Html.ActionLink("NRL", "Index", "NRL")</li>
        <li>@Html.ActionLink("State of Origin", "Index", "State of Origin")</li>
        <li>@Html.ActionLink("Cricket", "Index", "Cricket")</li>
        <li>@Html.ActionLink("Golf", "Index", "Gof")</li>
        <li>@Html.ActionLink("Motorsport", "index", "Motorsport")</li>

        <li>@Html.ActionLink("About", "About", "Home")</li>
        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
    </ul>
</nav>

行动方法

[ChildActionOnly]
public ActionResult NavigationMenu()
{
    string controller = ControllerContext.
                               ParentActionViewContext.
                               RouteData.Values["controller"].ToString();

    string action = ControllerContext.
                               ParentActionViewContext.
                               RouteData.Values["action"].ToString();

    if(controller == "Home" && action == "Index")
        return Content("");
    else
        return PartialView("_NavigationPartial");
}

渲染部分视图

@Html.Action("NavigationMenu", "Partial")

我没有测试过代码,但大部分内容应该没问题。