This project is read-only.

Displaying attached ContentItems in their own templates.

Topics: Troubleshooting
Mar 11, 2014 at 11:02 PM
I'm using Orchard 1.7.2.

I have a content definition of Special order, which has a content definition of Date Amended attached.

here's the code for Content-SpecialOrder.cshtml: (still under construction)
@using Orchard.Utility.Extensions;
    Layout.Title = Model.Title;
    var contentTypeClassName = ((string)Model.ContentItem.ContentType).HtmlClassify();

    WorkContext.Layout.SubPageTitle.Add(Model.Header, "10");

    var tag = Tag(Model, "article");

    var orderNumber = "";
    var subject = "";
    var dateFiled = "";
    var status = "";
    var documentPath = "";
    //var documentText = "";    

    foreach (dynamic part in Model.ContentItem.Parts)
        if (part.GetType().Name == "ContentPart")
            foreach (dynamic field in part.Fields)
                if (field.Name == "OrderNumber")
                    orderNumber = field.Value;

                if (field.Name == "Subject")
                    subject = field.Value;

                if (field.Name == "DateFiled") {
                    dateFiled = field.DateTime.ToString();

                if (field.Name == "Status")
                    status = field.Value;

                if (field.Name == "Document")
                    //documentPath = field.Path;
                    //documentText = field.Text;

        <td class="orderno">@orderNumber</td>
        <td class="subject">@Html.Raw(subject)</td>
        <td class="filed">@dateFiled</td>       
    <td class="amended">
            foreach (dynamic part in Model.ContentItem.Parts) {
                if (part.GetType().Name == "ContentPart") {
                    foreach (dynamic field in part.Fields) {
                        if (field.Name == "DateAmended") {
                            foreach (dynamic dateAmended in field.ContentItems) {
        <td class="status">

I made a Content-DateAmended.cshtml:
    var test = Model;
What I am trying to make happen is when it is trying to render the Date Amended Content Item I want it to hit Content-DateAmended.cshtml instead what happens is it renders as follows:


Can someone offer a suggestion on how I can accomplish what I am trying to do? or is it not possible at all?

Thank you for the help.
Mar 12, 2014 at 10:04 AM
According to your code, DateAmended is a field, probably a content picker field. Fields normally get rendered auto, but since you take over content rendering of SpecialOrder content type and removed all placement logic, ie. @Display(Model.Content) etc., they probably don't.

@Display method needs a shape to render. ContentPickerField's drivers would provide that shape, but you removed placement logic. ContentItems property does not provide shapes. If it did, and if a content item in that list were of type DateAmended, your template Content-DateAmended.cshtml would be hit.

What you can do is to call BuildDisplay on those content items to create content shapes, and call @Display on the generated shapes, as someone did here:

Alternatively, you can change the @Display(dateAmended) call to @Display(New.AShapeNameYouWant(ADynamicPropertyNameYouWant:dateAmended)). This creates a shape (not a content shape, an ad hoc shape) on the fly. You need to add a AShapeNameYouWant.cshtml to your theme, and in that template you will have Model.ADynamicPropertyNameYouWant will be the item. You *won't* be able to call @Display in this template either, but you can put in html tags and reuse the shape somehow.

Mar 13, 2014 at 5:21 PM
The field DateAmended is of DateAmended Content Type which is why I created the Content-DateAmended.cshtml

Thank you for the idea, I had to abandon it, I couldn't get it to work even after trying the other suggestion.
Mar 20, 2014 at 10:41 PM
Edited Mar 20, 2014 at 10:42 PM
So I realized what I was doing wrong:

I was trying to follow the example from the link posted:
but no Detail template exists thus the reason I couldn't get it to work, so what I did was this:
and it now works fine.

Thanks again for the help.