bug0r
bug0r

Reputation: 613

How do I get a ContentItem's content in Orchard?

I have instance of ContentItem and I'd like to get the contents of it. How do I do this?

Upvotes: 1

Views: 3176

Answers (2)

Say that your type is named Contact, has a BodyPart, and has a text field name Subject. If you are inside a content template like Content-Contact.cshtml, you can access the content item using Model.ContentItem. Casting it as dynamic will let Orchard give you some useful behavior to access its contents.

// necessary to access parts by name
@using Orchard.ContentManagement; 

@{
    // cast in order to have improved accessors
    dynamic contentItem = Model.ContentItem; 

    // access each part by its name
    string body = contentItem.BodyPart.Text;

    // access fields using the default part 
    string subject = contentItem.Contact.Subject.Value;
}

Upvotes: 6

Bertrand Le Roy
Bertrand Le Roy

Reputation: 17814

That is extremely vague. I'll take an example at random:

contentItem.As<BodyPart>().Text

will get you the contents of the body part if it has one.

Upvotes: 5

Related Questions