【发布时间】:2014-11-24 14:15:48
【问题描述】:
我正在尝试在使用Html.RenderPartial() 通过共享Partial View 呈现的视图中输出一些额外的<head> 元数据。
我的_Layout.cshtml 看起来像这样:
<head>
//...
@if (IsSectionDefined("AdditionalMeta")) {
RenderSection("AdditionalMeta");
}
//...
</head>
...我的共享视图 (_Title.cshtml) 如下所示:
@model TitleViewModel
@section AdditionalMeta {
@if (Model != null && Model.Title != null)
{
//additional <meta> tags using Model properties here
}
}
//...other irrelevant code
...和Index.cshtml 视图实现_Title.cshtml:
@model TitleViewModel
@{
Html.RenderPartial("_Title", Model);
}
虽然_Layout.cshtml 中的IsSectionDefined("AdditionalMeta") 返回false,但它不会输出任何内容 - 我尝试将“AdditionalMeta”@section 移动到Index.cshtml - 这使得_Layout.cshtml 中的IsSectionDefined("AdditionalMeta") 返回@987654338 @ 但给出以下错误:
以下部分已定义但尚未为布局页面“~/Views/Shared/_Layout.cshtml”呈现:“AdditionalMeta”。
我是否遗漏了什么或以错误的方式处理这个问题?谢谢!
编辑:Faby 的解决方案是正确的,前提是“AdditionalMeta”@section 在 Index.cshtml 而不是 _Title.cshtml 中声明
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor