I'm trying to output some additional <head> metadata in views which are rendered using Html.RenderPartial() via a shared Partial View.
My _Layout.cshtml looks like this:
<head>
//...
@if (IsSectionDefined("AdditionalMeta")) {
RenderSection("AdditionalMeta");
}
//...
</head>
...My shared view (_Title.cshtml) looks like this:
@model TitleViewModel
@section AdditionalMeta {
@if (Model != null && Model.Title != null)
{
//additional <meta> tags using Model properties here
}
}
//...other irrelevant code
...And Index.cshtml view implementing _Title.cshtml:
@model TitleViewModel
@{
Html.RenderPartial("_Title", Model);
}
This outputs nothing though as IsSectionDefined("AdditionalMeta") in _Layout.cshtml returns false - I've tried moving the "AdditionalMeta" @section to Index.cshtml instead - this makes IsSectionDefined("AdditionalMeta") in _Layout.cshtml return true but gives the following error:
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "AdditionalMeta".
Am I missing something or approaching this the wrong way? Thanks!
EDIT: Faby's solution is correct, provided that the "AdditionalMeta" @section is declared in Index.cshtml rather than _Title.cshtml