asp.net-mvc MVC 4:为什么“_Layout.cshtml”没有在主视图上呈现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22311690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4: Why is "_Layout.cshtml" not rendering on Home View?
提问by Analytic Lunatic
I'm new to MVC Development and have started a new MVC 4 application to build a sort of Personal Portfolio for practice. I cannot figure out why my _Layout.cshtmlis not rendering within my home or default Viewthough. I've already checked the following:
我是 MVC 开发的新手,并且已经启动了一个新的 MVC 4 应用程序来构建一种用于实践的个人投资组合。我不明白为什么我_Layout.cshtml没有在我的家中渲染或默认渲染View。我已经检查了以下内容:
- The
App_Start/RouteConfig.csfile has not been modified since the application was created. The
Views/Shared/_ViewStart.cshtmlhas the following (default):@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
App_Start/RouteConfig.cs自应用程序创建以来,该文件未被修改。在
Views/Shared/_ViewStart.cshtml具有以下(默认):@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
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">
☰
</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 & Conditions of Use</a>
</div>
</body>
</html>
~/Views/Shared/_Header.cshtml
~/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>
My Default View below loads, but with nothing present. The title changes to "Index", and the rest is a blank page. Shouldn't the ~/Views/Shared/_Layout.cshtmlbe rendering within this page in some manner?
下面加载了我的默认视图,但没有任何内容。标题变为“索引”,其余为空白页。不应该~/Views/Shared/_Layout.cshtml以某种方式在此页面中呈现吗?
With what I have currently set-up, when the shared _Layout.cshtmlloads within my Home/Index/View, I should see the Header image I have created and sourced on the shared view.
使用我目前的设置,当_Layout.cshtml我的Home/Index/视图中的共享加载时,我应该看到我在共享视图上创建和获取的标题图像。
Views/Home/Indx.cshtml
视图/主页/Indx.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
</div>
</body>
</html>
When I try Layout = "~/Views/Shared/_Layout.chtml";it errors out saying it could not be found at the designated path. I was semi-working off an example a Developer friend sent me, and with it they are not explicitly setting the Layout?
当我尝试Layout = "~/Views/Shared/_Layout.chtml";它时出错,说在指定的路径上找不到它。我正在完成一个开发人员朋友发给我的示例,并且他们没有明确设置Layout?
回答by Adween
Couple of things...
几件事...
If you have a view you are trying to render in a shared view then you do not need any of the HTML layout elements contained within the view you are putting it into. Lets start with really basic.
如果您有一个试图在共享视图中呈现的视图,那么您不需要将它放入的视图中包含的任何 HTML 布局元素。让我们从非常基本的开始。
View start
查看开始
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
_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>
Home View
主页视图
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
Now the home view gets plonked into where @RenderBody()appears. So index will be a heading 2 in the page. The title is passed in the ViewBagobject and set as the page title in the _Layout.cshtmlfile with the following @ViewBag.Title
现在主视图被插入@RenderBody()出现的位置。所以索引将是页面中的标题 2。标题在ViewBag对象中传递并设置为_Layout.cshtml文件中的页面标题,如下所示@ViewBag.Title
回答by Josh
Get rid of the following in your Index.cshtml
删除 Index.cshtml 中的以下内容
@{
Layout = null;
}
This code is telling the rendering engine to use no shared layout. You also won't need any of the HTML pre-amble (HTML, HEAD or BODY tag) in the Index.
此代码告诉渲染引擎不使用共享布局。您也不需要索引中的任何 HTML 前导码(HTML、HEAD 或 BODY 标签)。
回答by ken4z
Remove
消除
@{ Layout = null }
or explicitly set it to "~/Views/Shared/_Layout.cshtml"; This is less preferable though.
或明确设置为“~/Views/Shared/_Layout.cshtml”;不过,这不太可取。
It is overriding the _ViewStart behavior.
它覆盖了 _ViewStart 行为。
Also, you will not need to include the tags in your index.cshtml view because they are included with your _Layout.cshtml.
此外,您不需要在 index.cshtml 视图中包含标签,因为它们包含在您的 _Layout.cshtml 中。
Essentially what is happening is you are creating a wrapper page with _Layout where you put content that is common across your website (e.g. Header, footer, some navigation). Then you create a view, in this case index.cshtml, that forms the body of the page. _Layout.cshtml and Index.cshtml get compiled into a single page, with _Layout.cshtml acting like a wrapper around Index.cshtml.
基本上发生的事情是您正在创建一个带有 _Layout 的包装页面,您可以在其中放置网站中常见的内容(例如页眉、页脚、某些导航)。然后创建一个视图,在本例中为 index.cshtml,它构成了页面的主体。_Layout.cshtml 和 Index.cshtml 被编译成一个页面,_Layout.cshtml 就像是 Index.cshtml 的包装器。

