This project is read-only.

Is there any way to use specific field from Model.Content in Layout.cshtml

Topics: Administration, Customizing Orchard, Troubleshooting, Writing themes
Dec 7, 2012 at 11:30 AM
Edited Dec 7, 2012 at 11:33 AM

I know my question is very simmilar to , but I can't find any usefull for me in that discussion. 

Here's my problem:

Designer of site has decided that  some pages needs a little different color scheme and images in head zone. Simple to solve with css if only I could put an additional class onto outer div somehow.

Not into content zone, but outside of it.

To be more specific:

  1. First some configuration in Dashboard
    Content -> Content Types -> Page (edit) -> Add Field -> CssClass(Text Field)
  2. Then I choose specific page, edit it and write "my-class-name" into CssClass field.
  3. Now I'd like to use this field somehow in Layout.cshtm, to use it on specific div.
    NOT inside Content.cshtm or Content-Page.cshtml

Unfortunatley I couldn't find a way, despite the fact that I spent 4 hours googling around and testing different solutions.

At last I came out with the following "solution" that I put into Content-Page.cshtml:

var cssClass = "";
    foreach (var field in Model.ContentItem.Page.Fields)
        if (field.Name != "CssClass")
        cssClass = field.Value;

    if (!String.IsNullOrWhiteSpace(cssClass))
        using (Script.Head())
    <script type="text/javascript">
        $(document).ready(function () {


It works... but...
It's lame solution.
It depends on jQuery and also it does on client side something that should be done on server side.

Does anyone knows better?

Dec 7, 2012 at 12:50 PM

First of all I would access the field directly, like so:

var cssClassField = Model.ContentItem.Page.CssClass;

Next, use the HTML helper method called AddPageClassNames to add a css class name to the <html> tag like so:

if(cssClassField != null) {

Alternatively, you could set a value on the Layout object and use that value to render a class name on your layout-wrapper element:

if(cssClassField != null) {
   Layout.PageClassName = cssClassField.Value;

Should work.


Dec 7, 2012 at 12:52 PM

Update: if the first line of code throws an exception because the content item does not have a "Page" content part, simply break the line up into two:

var pagePart = Model.ContentItem.Page;
var cssClassField = pagePart != null ? pagePart.CssClass : null;
Dec 7, 2012 at 4:12 PM
Edited Dec 7, 2012 at 4:13 PM

Thank you very much. 

Selection part works perfectly, specialy second version:

var pagePart = Model.ContentItem.Page;
var cssClassField = pagePart != null ? pagePart.CssClass : null;

For some reason first version does not work. It seems to "AddPageClassNames" helper expects "HtmlHelper", while "Html" is of type "HtmlHelper<Dynamic>".
That's why I got  exception "'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'AddPageClassNames' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax."

But second code snippet works like a charm:


if (cssClassField != null)
        Layout.PageClassName = cssClassField.Value;


The only thing that I had to add is following code in my Layout.cshtml:


if (Model.PageClassName != null) {


Before I didn't know that Content.cshtml is actualy rendered BEFORE Layout.cshtml and that "Layout" object in subordinate views is actualy Model for Layout.cshtml.

Yep, still a lot to learn.

Thanx again and best regards, 


Dec 7, 2012 at 5:01 PM

Ah, in order to use the HTML helper methods (which are extension methods) you need to pass in strongly typed arguments that are not dynamic. So in your case, this should work:


And your current solution is fine as well of course.