Jam*_*ith 34 asp.net asp.net-mvc razor asp.net-mvc-3
我的问题是,@RenderSection当@RenderSection在基本模板中定义时,我似乎无法使用嵌套模板.目前,我有一个嵌套的基本模板,它链接到子模板,然后在视图页面中使用.当我@RenderSection在基本模板中定义并在视图页面中呈现它时,它会抛出错误.
这是确切的问题.
我想创建一个RenderSection,允许我插入自定义脚本.我的基本模板......
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@RenderSection("HeaderContent", false) // The region of the header scripts (custom css)
</head>
<body>
@RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后我跳过子模板,因为我不想在其中放置任何自定义头部代码并将其应用于页面本身.
@section HeaderContent {
<script>alert("hi");</script>
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我似乎无法从我的普通页面添加自定义头代码到基本模板.
已定义以下部分,但尚未为布局页面呈现~/Views/Shared/OneColLayer.cshtml": "HeaderContent.
我是否需要在视图页面中包含指向基本模板的指针?
@{
Layout = "~/Views/Shared/BaseTemplate.cshtml";
}
Run Code Online (Sandbox Code Playgroud)
我的新基础模板
<head>
<link rel="stylesheet" type="text/css" href="@Url.Content("~/content/layout.css")" />
<link rel="stylesheet" type="text/css" href="@Url.Content("~/content/global.css")" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/js/fadeInFadeOut.js")"></script>
<title>@ViewBag.Title</title>
@RenderSection("HeaderContent", false)
</head>
<body>
@RenderBody()
</body>
Run Code Online (Sandbox Code Playgroud)
我的新儿童模板
@{
Layout = "~/Views/Shared/BaseTemplate.cshtml";
}
@RenderSection("HeaderContent", false)
@RenderBody()
Run Code Online (Sandbox Code Playgroud)
我的看法
@{
ViewBag.Title = "Home";
Layout = "~/Views/Shared/OneColLayer.cshtml";
}
@section HeaderContent {
<h1>Left Content</h1>
}
<div>my view content</div>
Run Code Online (Sandbox Code Playgroud)
内容现在放在oneCol模板的基本模板中.
结果...
<div id="Content">
<h1>Left Content</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
Nic*_*sen 55
您需要指定允许在中间模板中传递的部分.
BaseTemplate.cshtml
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@RenderSection("HeaderContent", false) @* The region of the header scripts (custom css) *@
</head>
<body>
@RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑
你的新儿童模板
@{
Layout = "~/Views/Shared/BaseTemplate.cshtml";
}
@section HeaderContent {
@RenderSection("HeaderContent", false)
}
@RenderBody()
Run Code Online (Sandbox Code Playgroud)
如果将渲染部分放在基础模板的某个部分内,它会将该部分渲染到基础模板上的正确位置.
View.cshtml - >使用MiddleLayout.cshtml作为它的布局
@section HeaderContent
{
<!-- header content that will now render -->
}
<!-- page content -->
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24304 次 |
| 最近记录: |