【问题标题】:MVC 4: Why is "_Layout.cshtml" not rendering on Home View?MVC 4:为什么“_Layout.cshtml”不在主视图上呈现?
【发布时间】:2014-03-11 14:13:52
【问题描述】:

我是 MVC 开发的新手,并且已经启动了一个新的 MVC 4 应用程序来构建一种用于练习的个人投资组合。我无法弄清楚为什么我的_Layout.cshtml 没有在我的家中渲染或默认View。我已经检查了以下内容:

  1. App_Start/RouteConfig.cs 文件自 应用程序已创建。
  2. Views/Shared/_ViewStart.cshtml 有 以下(默认):

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

视图/共享/_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>

~/Views/Shared/_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>

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

根据我目前的设置,当共享的_Layout.cshtml 在我的Home/Index/ 视图中加载时,我应该会看到我在共享视图中创建并获取的标题图像。

Views/Home/Indx.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

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

    </div>
</body>
</html>

当我尝试Layout = "~/Views/Shared/_Layout.chtml"; 时,它会出错,提示无法在指定路径上找到它。我正在开发一个开发人员朋友发给我的示例,他们没有明确设置Layout?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    几件事...

    如果您有一个试图在共享视图中呈现的视图,那么您不需要将它放入的视图中包含的任何 HTML 布局元素。让我们从最基础的开始吧。

    查看开始

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

    _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>
    

    主页视图

    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    

    现在主页视图被插入@RenderBody() 出现的位置。所以 index 将是页面中的标题 2。标题在ViewBag 对象中传递,并设置为_Layout.cshtml 文件中的页面标题,并带有以下@ViewBag.Title

    【讨论】:

      【解决方案2】:

      在您的 Index.cshtml 中删除以下内容

      @{
          Layout = null;
      }
      

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

      【讨论】:

      • 做到了 :) 谢谢!
      【解决方案3】:

      删除

      @{ Layout = null } 
      

      或将其显式设置为“~/Views/Shared/_Layout.cshtml”;不过,这不太可取。

      它覆盖了 _ViewStart 行为。

      此外,您不需要在 index.cshtml 视图中包含标签,因为它们包含在您的 _Layout.cshtml 中。

      基本上发生的事情是您正在使用 _Layout 创建一个包装页面,您可以在其中放置您网站中常见的内容(例如页眉、页脚、一些导航)。然后创建一个视图,在本例中为 index.cshtml,它构成了页面的主体。 _Layout.cshtml 和 Index.cshtml 被编译成一个页面,而 _Layout.cshtml 就像 Index.cshtml 的包装器一样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        相关资源
        最近更新 更多