Model.ContentItem - within Layout.cshtml

Topics: Writing themes
Sep 3, 2012 at 9:00 PM

Why would this always be null from the Layout.cshtml?

I have added a new text field to the Page Content Type and would like to use the value of that text field to determine a class name; but everytime I reference Model.ContentItem - it's always null.

What's the best way to access this field (assume the name of the field is ClassName )

Any help is appreciated.


Sep 5, 2012 at 5:03 PM

Discovered that I need to add a line of code to the Content-Page.cshtml (same one that sets the page Title) and set the dynamic variable "Layout" to support ContentItem.

(e.g. Layout.ContentItem = Model.ContentItem)

Now I have access to ContentItem from within the Layout.cshtml.

When not using the Content-Page.cshtml alternate; the ContentItem is still null; so I'm not sure how everyone else's is not null unless they are setting it elsewhere.

Hope this helps someone.


Sep 12, 2012 at 9:53 PM

Because there is no content associated with Layout unless you do what you did in your second post. The content item gets rendered in the Content zone that is in layout.

Everyone else's *is* null.

Sep 12, 2012 at 11:43 PM

lol - thanks!