How do I get a ContentItem's content in Orchard?
I have instance of ContentItem a开发者_高级运维nd I'd like to get the contents of it. How do I do this?
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;
}
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.
精彩评论