This project is read-only.

Displaying custom field

Topics: Customizing Orchard, General
May 20, 2013 at 12:31 PM
Hi there

I have a content type called Property and within this I have a custom field called Location.
I am trying to display this by using the following code: - @Display(Model.Property.Location) but this is not working. Could anyone give me any ideas on how to do this please?

Many thanks,
May 21, 2013 at 9:27 AM
Hi, I am still wrangling with this one! Could anyone help or give any tips please?

May 21, 2013 at 9:36 AM
The Display method cannot be used to render fields; it is intended to render shapes. So if you want to render a field's value directly, simply render it out like this (assuming the custom field has a Value property; if you're not sure, lookup the class definition of the field):
May 21, 2013 at 9:59 AM

Thanks again.

I have tried this and it does not work. Please could you tell me how to look up the class definition of the field?

Thanks, Kirsty

May 21, 2013 at 10:05 AM
Sure, if you tell me what field type you are using.
May 21, 2013 at 10:08 AM

Thanks J Location is a text field.

May 21, 2013 at 10:15 AM
Ok. TextField lives in Orchard.Core/Common/Fields/TextField.cs (friendly hint: Visual Studio contains a search function which can be of great help to find definitions).
As you'll see, TextField contains a Value property, so something else must be wrong.

The next question is: in what template are you executing said code?
May 21, 2013 at 10:18 AM

Excellent, thanks very much. I am using Web Matrix rather than Visual Studio, maybe I should change.

The template name I am using is Content-Property.Detail.cshtml

(I am trying to create some info to be displayed in property detail view).


May 21, 2013 at 10:29 AM
Aha, so that is a Content template. The Model of a content template contains a property called ContentItem, from which in turn you can access its parts and the fields of those parts. If you attached the Location field to the Property content item via the admin, Orchard will have created a so called implicit part which has the same name as the content type. You should be able to access it like this:
May 21, 2013 at 10:32 AM

Fantastic!! It works. Truly appreciate it, thank you very much! I can move on to my next challenge now! Kirsty

May 21, 2013 at 10:35 AM
You're welcome. :)
May 21, 2013 at 10:45 AM
Edited May 21, 2013 at 10:46 AM
kirstybash wrote:
I am trying to display this by using the following code: - @Display(Model.Property.Location) but this is not working. Could anyone give me any ideas on how to do this please?
Is there a reason why you were trying to display it that way though? Is your syntax wrong or are you just after a value? :)

For each field and part that are listed and valid by the rules in the placement it will create the shape(s) for the Display(Model.{Zone}) like the theme does.

What you are up to reminds me of a bit more of a descriptive template like:
Easy content templates

In that case the content type template wants to look like:
<div class="Property">
</div> detail:
Lets place Orchard's text field in the Location Zone
<Match ContentType="Property">
  <Match DisplayType="Detail">
      <Place Fields_Common_Text-Location="Location:0"></Place>
Jun 13, 2013 at 10:31 AM
Hi trying to display an image from Media Picker in the same way as above but I get an error saying 'Orchard.Fields.Fields.MediaPickerField' does not contain a definition for 'Value'

I was trying the following code - @Model.ContentItem.Property.PDF.Value Any ideas how to display this please?

Tx Kirsty
Jun 13, 2013 at 10:59 AM
Got this to work by adding @Model.ContentItem.Property.PDF.Url however now have the problem that the url is adding ~ at the beginning, how do you remove this please?
Jun 15, 2013 at 7:44 AM
Jun 17, 2013 at 11:19 AM

Thankyou SO much! This works a treat.