This project is read-only.

Struggling to customise display of summary views on homepage

Topics: Customizing Orchard, General
Dec 20, 2014 at 3:46 PM
Edited Dec 20, 2014 at 3:51 PM

I am trying to create an alternate view for a couple of content types on the homepage.

One is using a custom "Course" content type and the other is for BlogPost.

I have already created an alternate Content-Course.Summary and this works for customising the view of courses on the course list. However I am trying to make Content-Course-url-homepagel.Summary and for the life of me can't work out how to correctly access the fields from the model. I very simply want to access the "Title" and "Summary" fields which are both text fields.

Whenever and whatever I do I get object not set errors on "content" in the ItemDisplayLink method.

I would imaging I need to access the model the same way for both of these scenarios, as I get exactly the same error when trying to create the same alternate for BlogPosts "Content-BlogPost-url-homepage.Summary".

Here is my code for example, I have tried many alternatives and have ready many topics discussing specifically this instance and this particular alternate but none of the code described has worked so far. I will mention I am using 1.8.1.
@using Orchard.Utility.Extensions;
    Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
    string title = Model.Title.ToString();
    string bodyHtml = Model.Html.ToString();
    var body = new HtmlString(Html.Excerpt(bodyHtml, 200).ToString()
        .Replace(Environment.NewLine, "</p>" + Environment.NewLine + "<p>"));

<h1>@Html.ItemDisplayLink(title, contentItem)</h1>

<p>@body @Html.ItemDisplayLink(T("more").ToString(), contentItem)</p>
I will add I have been using the shape tracing tools extensively and I think the above code was actually provided by that and selecting the specific fields I was interested in.

Many thanks

Dec 21, 2014 at 7:44 AM
Edited Dec 22, 2014 at 12:19 AM
The Model depends of the base alternate you use. You can use directly Model.Title if you are based on the Core/Title/Views/Parts.Title_Summary.cshtml (for overriding in your theme, you can omit the "Parts." if you put your *.cshtml file in the YourTheme/Views/Parts subfolder). For the body, you can use Model.Html if you are based on the Core/Common/Views/Parts.Common.Body.Summary... You can see that in the Shape Tracing

In your case, it seems that you are based on the Core/Content/Views/Content.Summary.cshtml. It's a parent shape that pass others models to its children. Take a look at this file and Shape Tracing. Here, you can access the contentItem and the title like that
    Orchard.ContentManagement.ContentItem contentItem = Model.ContentItem;
    string title = Model.ContentItem.TitlePart.Title.ToString();
    string bodyHtml = Model.ContentItem.BodyPart.Text.ToString();
    var body = new HtmlString(Html.Excerpt(bodyHtml, 200).ToString().Replace(Environment.NewLine, "</p>" + Environment.NewLine + "<p>")); 
Note: Here, it's good also to add some null checking on the title and body parts (e.g if Model...BodyPart != null)

But maybe, you have better to override the title and body summary alternates to do what you want with these parts with your content type... For the content summary alternate, it's good also to keep the original structure as the <article> tag and its classes...


Here, because of the use of the .ItemDisplayLink() extension, you need strongly typed variables, e.g title (string) and contentItem (ContentItem). So, you can also use the .As<TPart>() extension of the ContentItem class. This way you can better test, for example, if there is no body part. This, because I just tried with a content type without body part, and the program throw an exception if you have this test
if (Model.ContentItem.BodyPart != null)
Here, an example with the .As() extension, a null checking on the body part, and the required using declarations
@using Orchard.ContentManagement
@using Orchard.Core.Title.Models;
@using Orchard.Core.Common.Models;
    var contentItem = (ContentItem)Model.ContentItem;
    var title = contentItem.As<TitlePart>().Title.ToString();

    var bodyHtml = string.Empty;
    if (contentItem.As<BodyPart>() != null) {
        bodyHtml = contentItem.As<BodyPart>().Text.ToString();
Marked as answer by mcinnes01 on 12/23/2014 at 2:11 AM
Dec 23, 2014 at 10:12 AM
Hi jtkech,

This worked a treat and thanks for the fantastic explanation.

Many thanks