【发布时间】:2020-04-15 06:36:38
【问题描述】:
在 MVC 中如何访问 _layout.cshtml 中的单个视图?
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ContactUs()
{
return View();
}
public ActionResult Header()
{
return View();
}
}
当我点击添加视图(标题和联系人)时,然后选择此视图,如下所示。 ContactUs.cshtml
@{
ViewBag.Title = "ContactUs";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ContactUs</h2>
@section ContactUs{
<h1>this is contact view</h1>
}
Header.cshtml
@{
ViewBag.Title = "Header";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Header</h2>
@section Header{
<h1>this is header view</h1>
}
_Layout.cshtml
<body>
<div class="container body-content">
@RenderSection("Header") //Additional information: Section not defined: "Header".
@RenderBody()
@RenderSection("ContactUs") //Additional information: Section not defined: "ContactUs".
<footer>
<p>2016@CopyRightsReserved</p>
</footer>
</div>
@RenderSection("")
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</body>
//附加信息:未定义部分:“标题”。获取错误运行时
我做错了什么??
【问题讨论】:
标签: c# asp.net-mvc layout