How to access one ContentPart from another ContentPart from within the same ContentType

Topics: Customizing Orchard, Writing modules
Dec 19, 2012 at 9:53 PM

Hi there.

I'm trying to use the model from one Part in the View of a sibling Part.

Here's a little background code

migrations.cs

ContentDefinitionManager.AlterPartDefinition("BallsPart", cfg => cfg
    .Attachable()
);
ContentDefinitionManager.AlterPartDefinition("BlueBallsWidgetPart", cfg => cfg
    .Attachable()
);
ContentDefinitionManager.AlterTypeDefinition("BlueBallsWidget",cfg => cfg
    .WithPart("BlueBallsWidgetPart")
    .WithPart("BallsPart")
    .WithPart("CommonPart")
    .WithPart("WidgetPart")
    .WithSetting("Stereotype", "Widget")
);

 

BlueBallsWidgetDriver.cs

public class BlueBallsWidgetDriver : ContentPartDriver<BlueBallsWidgetPart> {
    protected override DriverResult Display(BlueBallsWidgetPart part, string displayType, dynamic shapeHelper) {
        return ContentShape("Parts_BlueBallsWidget",
            () => {
                var shape = shapeHelper.Parts_BlueBallsWidget();
                shape.ContentPart = part;
                return shape;
            }
        );
    }
}

 

BlueBallsWidget.cshtml

    class="balls"> @foreach (var ball in Model.ContentPart.ContentItem.BallsPart.ListOfBlueBalls) {
  • . . }

 

When I run this, I get an error: 'Orchard.ContentManagement.ContentPart' does not contain a definition for 'ListOfBlueBalls'.

What's irksome about this whole thing is that I had this thing *working*. And then I did some cleanup and now it doesn't work anymore.

Any wisdom would be much appreciated.

Thanks,

Coordinator
Dec 19, 2012 at 10:17 PM

((IContent)Model.ContentPart).As<BallsPart>().ListOfBlueBalls should work, although you didn't show what's in BallsPart.

Dec 20, 2012 at 6:06 AM

Thanks for responding.

Let me make sure I'm clear on what this is doing.

Model.ContentPart is type BlueBallsWidgetPart. We're casting Model.ContentPart as a IContent. We then call As<> on the IContent, casting as type BallPart. And then we access member ListOfBlueBalls. Do I have this correct?

I'm unclear how we're accessing another Part included under the same ContentType (BlueBallsWidget).

Developer
Dec 20, 2012 at 9:55 AM

You have it correct (although I did not verify the exact structure of your content type, but I trust you live and breath that), and that is how you access different parts of a content item. In Bertrand's example, he's accessing the BallsPart of the content item of which BlueBallsWidgetPart is also a part (checkout the implementation of the As<TPart> extension method to see how it's done).

Dec 30, 2012 at 8:46 AM

This code worked... well sort of. I was able to access different parts belonging to a content item. I tested it with the Common part and it came up just fine. However, the part I was interested in, BallsPart, never did show up. I finally decided it wasn't being added properly to the content item.

Given I have so little time to work on this, I decided to do this a completely different way and got along without needing to use Bertrand's code. Eh, but I'll know it for the next time I need it.  :-)

Thanks for helping.