MVC 4:为什么"_Layout.cshtml"无法在Home View上呈现?

Ana*_*tic 1 asp.net-mvc razor asp.net-mvc-4

我是MVC Development的新手,已经开始使用新的MVC 4应用程序来构建一种个人投资组合.我无法弄清楚为什么我_Layout.cshtml不在我的家中渲染或默认View.我已经检查了以下内容:

  1. App_Start/RouteConfig.cs文件自创建应用没有被修改.
  2. Views/Shared/_ViewStart.cshtml具有以下(默认):

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    Run Code Online (Sandbox Code Playgroud)

查看/共享/ _Layout.cshtml:

        <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale = 1.0" />
            <title>Portfolio</title>
            @Styles.Render("~/Content/css")
            @Scripts.Render("~/bundles/jquery")
            @Scripts.Render("~/bundles/bootstrapjs")
            @Scripts.Render("~/bundles/customJs")
            <link href="~/Content/bootstrap.css" rel="stylesheet" />
            <link href="~/Content/bootstrapMod.css" rel="stylesheet" />
            <link href="~/Content/Site.css" rel="stylesheet" />
        </head>
        <body style=""zoom: 1">
            <div class="container mainContent">
                <div class="page-header hidden-sm hidden-xs">
                    @Html.Partial("_Header")
                </div>

                <nav id="navBar" class="navbar navbar-default" role="navigation">
                    <div class="container-fluid">
                        <!-- Brand and toggle get grouped for better mobile display -->
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
                                &#9776;
                            </button>
                            <a class="navbar-brand hidden-lg hidden-md" href="/Home">Portfolio</a>
                        </div>

                        <!-- Collect the nav links, forms, and other content for toggling -->
                        <div class="collapse navbar-collapse" id="navbar-collapse">                    
                           @*@Html.MvcSiteMap().Menu()*@

                        </div>
                    </div>
                </nav>

                <div class="container">
                    @RenderSection("scripts", required:false)

                    <div class="pageTitle largeFontSize">
                        @ViewBag.HeaderTitle
                    </div>

                    @RenderBody()
                </div>


            </div>
            <div id="footer">
                Copyright 2014 - Portfolio - <a target="_blank" href="/terms-and-conditions-of-us.html">Website Terms &amp; Conditions of Use</a>
            </div>
        </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

〜/查看/共享/ _Header.cshtml

<div class="row">
    <div class="col-md-6">
        <div>
            <a href="~/Home">
                <img src="~/Content/Images/Header.png" class="img-responsive"/>
            </a>
        </div>
    </div>
    <div class="col-md-6 box">
        @if (Request.IsAuthenticated)
        {
            <div class="profileLinks">                    
                <a href="#">My Projects</a>
                |
                <a href="#">Contact Profile</a>
            </div>

            <div class="content logout">                
                <div class="row">
                    <div>
                        Hi, @User.Identity.Name
                        @Html.ActionLink("Log Off", "LogOff", "Account", new {},new { @class = "btn btn-primary btn-sm" })
                    </div>
                </div>                
            </div>
        }
        else
        {
            <div class="content">
                <div class="row">
                    <div class="col-md-4">
                        <input type="text" id="username" placeholder="E-mail" required tabindex="1"/>
                        <a href="#" ><h6>Forgot your username?</h6></a>
                    </div>
                    <div class="col-md-4">
                        <input type="password" id="password" placeholder="Password" required tabindex="2" onkeyup="EnterKeyPressed()"/>
                        <a href="#" ><h6>Forgot your password?</h6></a>
                    </div>
                    <div class="col-md-4">
                        <input type="button" id="loginButton" value="Login" onclick="login()" class="btn btn-primary btn-sm" tabindex="3" onkeyup="EnterKeyPressed()"/>
                        <br />
                        <input type="checkbox" id="rememberMe">Remember Me
                    </div>
                </div>
            </div>
        }
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的默认视图加载,但没有任何内容.标题更改为"索引",其余为空白页.不应该~/Views/Shared/_Layout.cshtml以某种方式在此页面内呈现?

根据我目前设置的内容,当_Layout.cshtml我在Home/Index/View中共享加载时,我应该看到我在共享视图上创建并获取的Header图像.

查看/主页/ Indx.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>

    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我尝试Layout = "~/Views/Shared/_Layout.chtml";它时错误地说它在指定的路径上找不到.我是开发人员朋友寄给我的一个例子,并且有了它,他们没有明确地设置Layout

Adw*_*een 7

几件事......

如果您有一个视图,您尝试在共享视图中呈现,那么您不需要将其放入视图中的任何HTML布局元素.让我们从真正的基础开始.

查看开始

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

_Layout.cshtml

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>@ViewBag.Title</title>
</head>
<body>
    @RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

家庭观点

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
Run Code Online (Sandbox Code Playgroud)

现在,主页视图会@RenderBody()显示在哪里.因此索引将是页面中的标题2.标题在ViewBag对象中传递,并在_Layout.cshtml文件中设置为页面标题,具有以下内容@ViewBag.Title


Jos*_*osh 5

删除 Index.cshtml 中的以下内容

@{
    Layout = null;
}
Run Code Online (Sandbox Code Playgroud)

此代码告诉渲染引擎不使用共享布局。您也不需要索引中的任何 HTML 前导码(HTML、HEAD 或 BODY 标签)。