This sounds like a (very) silly question but I just can’t find a way to access a text field I’ve just added to a content type in Orchard. I know how to access the field when it is added to a content part, but not when it is added directly to the content type itself.
The content type is called Product, it is containable and is being shown in a list. I’ve added the ordinary parts, plus two custom fields: a MediaPicker field named “Image” and a Text field with HTML flavor named “About”. The code I’m using to render the list which contains it is the following:
@using Orchard.DisplayManagement.Shapes;
@{
var list = Model.List;
var items = list.Items;
var count = items.Count;
var listTag = Tag(list, "ul");
listTag.AddCssClass("historia");
var index = 0;
}
<div class="produtos">
@foreach (var item in items)
{
var itemTag = Tag(item, "div");
itemTag.AddCssClass("item-" + index);
@itemTag.StartElement;
<h3>@item.ContentItem.TitlePart.Title</h3>
<img src="@Href(item.Image.Url)"
class="@item.Image.Class"
alt="@item.Image.AlternateText"
width="@item.Image.Width"
height="@item.Image.Height"
style="@item.Image.Style" />
<p>@item.About.Text</p>
@itemTag.EndElement;
++index;
}
</div>
@Display(Model.List)
@Display(Model.Pager)
The problem is that @item.Image does not yeilds the Image field, nor does @item.About yields the “About” text field. I don’t even know if a Text field has a .Text property to access its value, after all it can have HTML flavor… I don’t even know where to look in the source code to help me finding out how this is done!
I’ve tried some different approaches like @item.ContentFields.About.Text or @item.ContentField.About.Text, @item.Fields[0], @item.ContentItem.About.Text, @item.ContentItem.ContentFields.About.Text (the last two throws an exception telling me that ContentItem doesn’t have those fields…)
I know that if I had added the fields to a content part and this part to the content type I could have done this: @item.ContentItem.TheNameOfTheContentPart.Image.Url …
I’m becoming more and more frustrated as I can’t figure out how to do the simplest things in Orchard. I need that Eureka moment that will make me able to know where to look at in the source code to find answers to these kind of questions.
A field is always under a part. It’s just that if you add the field from the admin, a part with the same name as the type will be created automatically for you.
http://orchard.codeplex.com/discussions/396280
You can’t add a field to a content type, you always add it to a content part. It’s just that if that part has the same name as the type, nice magical things happen.
http://orchard.codeplex.com/discussions/397426