DEV Community

Discussion on: Kentico 12: Design Patterns Part 6 - Rendering Meta Tags in Kentico 12 MVC

Collapse
 
seangwright profile image
Sean G. Wright • Edited

@oliverfurmage

I believe for Template pages you need to specify a Layout, which means whatever you've done for your primary site _Layout.cshtml you will need to do in the Template page's layout as well to get Site Meta information to work correctly.

ViewBag should work fine even in this scenario (however I haven't actually tested it).


On the docs page for MVC Page Templates it mentions needing to specify a layout for the template Razor file.

Use MVC layouts with the template view for any shared output code (based on your requirements, you can use your site's main layout, a dedicated layout for page templates, etc.).

This can be done through an explicit definition at the top of the Razor file:

@{
    ViewBag.Title = "Home Page";
    Layout = "~/Views/Shared/_myLayoutPage.cshtml";
}

Or through a _ViewStart.cshtml file to set the layout for an entire subfolder.

Collapse
 
oliverfurmage profile image
oliverfurmage

@seangwright

I had already set the Layout of the template file and I could see elements from my _Layout file (for example the header), however it seems to me that the ViewBag variables set in the controller were not being passed through.

To get round this I decided to do the following on the template file;

@{
    ViewBag.Title = Model.Page.DocumentName;
    Layout = "~/Shared/Views/_Layout.cshtml";
    ViewBag.MetaPageDescription = Model.Page.GetInheritedValue("DocumentPageDescription");
    ViewBag.MetaPageKeyWords = Model.Page.GetInheritedValue("DocumentPageKeyWords");
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
seangwright profile image
Sean G. Wright

Interesting! Good to know there is a solution like this 🧐.