0

我在 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 文件中会引发此错误。这应该如何补救?

4

1 回答 1

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")

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

于 2013-05-15T22:46:12.600 回答