@RenderSection嵌套剃刀模板 [英] @RenderSection in nested razor templates

查看:204
本文介绍了@RenderSection嵌套剃刀模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我似乎无法使用 @RenderSection 从嵌套模板时定义 @RenderSection 在基座模板。目前,我有一个链接到一个子模板,然后在视图页面中使用嵌套基本模板。当我在基础模板定义 @RenderSection ,呈现在视图页面,它抛出一个错误。

下面是确切的问题。

我想创建一个RenderSection让我插入自定义脚本。
我的基本模板....

 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<标题> @ ViewBag.Title< /标题>
 @RenderSection(HeaderContent,FALSE)//头脚本的区域(自定义CSS)< /头>
<身体GT;
@RenderBody()
< /身体GT;
< / HTML>

然后我跳过子模板,因为我不想把自定义的头code在那里,并将其应用到页面本身。

  @section HeaderContent {
    <脚本>警报(你好);< / SCRIPT>
}

我的问题是,我似乎从我的正常网页中的基本模板添加自定义头code。

下面的路段已确定,但尚未呈现的布局页面〜/查看/共享/ OneColLayer.cshtml:HeaderContent

我需要包括一个指向该视图页面?

基础模板

  @ {
    布局=〜/查看/共享/ BaseTemplate.cshtml
}

我的新的基本模板

 < HEAD>
  <链接rel =stylesheet属性类型=文/ CSS的href =@ Url.Content(〜/内容/ layout.css中)/>
  <链接rel =stylesheet属性类型=文/ CSS的href =@ Url.Content(〜/内容/ global.css)/>
  <脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/脚本/ jQuery的-1.5.1.min.js)>< / SCRIPT>
  <脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/ JS / fadeInFadeOut.js)>< / SCRIPT>
  <标题> @ ViewBag.Title< /标题>
  @RenderSection(HeaderContent,FALSE)
< /头>
<身体GT;
  @RenderBody()
< /身体GT;

我的新子模板

  @ {
  布局=〜/查看/共享/ BaseTemplate.cshtml
}
@RenderSection(HeaderContent,FALSE)
@RenderBody()

我的看法

  @ {
  ViewBag.Title =家;
  布局=〜/查看/共享/ OneColLayer.cshtml
}
@section HeaderContent {
  < H1>左内容及LT; / H1>
}
< D​​IV>我认为内容和LT; / DIV>

内容被放置在oneCol模板现在的基本模板。

结果...

 < D​​IV ID =内容>
   < H1>左内容及LT; / H1>
< / DIV>


解决方案

您需要指定允许在中间模板通过部分。

BaseTemplate.cshtml

 <!DOCTYPE HTML>
< HTML和GT;
  < HEAD>
    <标题> @ ViewBag.Title< /标题>
    @RenderSection(HeaderContent,FALSE)@ *头脚本(自定义CSS)*的区域@
  < /头>
<身体GT;
  @RenderBody()
< /身体GT;
< / HTML>


修改

新的子模板

  @ {
  布局=〜/查看/共享/ BaseTemplate.cshtml
}
@section HeaderContent {
  @RenderSection(HeaderContent,FALSE)
}
@RenderBody()

如果你把渲染部分从基本模板课里面,它会呈现在基础模板正确的地方部分。


View.cshtml - >使用MiddleLayout.cshtml,因为它的布局

  @section HeaderContent
{
    <! - 头部的内容,现在将呈现 - >
}<! - 页面内容 - >

My problem is I can't seem to use @RenderSection from a nested template when @RenderSection is defined in the base template. Currently, I have a nested base template which is linked to a child template which is then used in the view pages. When I define the @RenderSection in the base template and render it in the view pages it throws an error.

Here's the exact problem.

I want to create a RenderSection to allow me to insert custom scripts. My base template....

<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
 @RenderSection("HeaderContent", false) // The region of the header scripts (custom css)

</head>
<body>
@RenderBody()
</body>
</html>

I then skip the child template as I do not want to put any custom head code in there and apply it to the page itself..

@section HeaderContent {
    <script>alert("hi");</script>
}

My problem is that I cant seem to add custom head code in to the base template from my normal pages.

The following sections have been defined but have not been rendered for the layout page ~/Views/Shared/OneColLayer.cshtml": "HeaderContent.

Do I need to include a pointer to the base template in the view page?

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

My new base template

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

my new child template

@{
  Layout = "~/Views/Shared/BaseTemplate.cshtml";
}
@RenderSection("HeaderContent", false)
@RenderBody()

my view

@{
  ViewBag.Title = "Home";
  Layout = "~/Views/Shared/OneColLayer.cshtml";
}
@section HeaderContent {
  <h1>Left Content</h1>
}
<div>my view content</div>

the content gets placed in the oneCol template now the base template.

results...

<div id="Content">
   <h1>Left Content</h1>
</div>

解决方案

You need to specify the sections that are allowed to pass through in the middle template.

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>


EDIT

your new child template

@{
  Layout = "~/Views/Shared/BaseTemplate.cshtml";
}
@section HeaderContent {
  @RenderSection("HeaderContent", false)
}
@RenderBody()

If you put the render section inside of a section from the base template, it will render that section in the correct place on the base template.


View.cshtml -> uses MiddleLayout.cshtml as it's layout

@section HeaderContent
{
    <!-- header content that will now render -->
}

<!-- page content -->

这篇关于@RenderSection嵌套剃刀模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆