Getting value a taxonomy field.

Topics: Customizing Orchard, Writing themes
Oct 10, 2011 at 11:11 PM

Hi,

I created a new content type which include a Taxonomy Field called Client (setup to select in the "Clients" taxonomy).

I my summary view, I am trying to display the selected Client. I thought that

 

string client = Model.ContentItem.Campaign.Client.Name;

would return me the name of the client, but instead it returns me the name of the taxonomy field (ie: Client)

What am I missing?

Thanks,

Mathieu

 

Oct 12, 2011 at 6:55 PM

I am still scratching my head with this one. Would love any help or direction.

Thanks

Coordinator
Oct 13, 2011 at 6:42 PM

The field itself doesn't contain any storage, and the Name property you are accessing is the one from ContentField, so any field has it.

What you want is to to access the TermsPart from the content item, which links to all terms attached to it.

For instance:
var termPart = Model.ContentItem.TermsPart.Terms.Single(x => x.Field == "Client");
dynamic termContentItem =  termPart.ContentItem;
string client = termContentItem.RoutePart.Title;


 

Oct 13, 2011 at 7:02 PM

First of all, thanks for getting back to me.

I have tried your solution, and now I am getting this error:

  CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Referring to  x => x.Field == "Client"

I am sorry, I have little to none experience with dynamic LINQ.

Coordinator
Oct 13, 2011 at 7:18 PM

Actually it won't work like this ... give me some more minutes to see what I can do to make it easier.

Coordinator
Oct 13, 2011 at 10:23 PM

I have uploaded some new code to the dev branch (it means you need to work based on the source code version).

This lets you do this:

@foreach(var term in Model.ContentItem.Campaign.Client.Terms.Value) {
    @term.Name
}

Remember that a field can have multiple terms attached.

Oct 13, 2011 at 10:57 PM

I might be doing something wrong here.

I downloaded the latest source code of the Taxonomy module and replaced the current module I had (v.0.9) by this one.

Unfortunately it brought me this error:

System.Data.SqlClient.SqlException: Invalid object name 'Contrib_Taxonomies_TermsPartRecord'.

I also tried to just changed the two files you updated. But once again it complained about the 

IRepository<TermsPartRecord> repository,

Merci encore pour l'aide!

Coordinator
Oct 14, 2011 at 12:15 AM

The dev version is completely incompatible with the previous one. Only solution is to remove every table and start again.

Oct 14, 2011 at 7:18 PM

Alright, maybe I'll do that then.

Thanks for your help, I'll update the post when I have this working.

Apr 19, 2012 at 5:07 PM

I am pulling my hair out trying to simply display the value on a taxonomy that is associated with a content item.

I have a taxonomy called "Department" and I have a custom content item called "ProjectFile" that can be associated with one or more departments.

Using shape tracing I see that 

is able to render the values correctly. The Model tab in shape tracing shows the Model as:

@Model.Terms[0].Name

however when I use "@Model.Terms[0].Name" in another view ( i want to use it in ~/Themes/Bootstrap/Views/Content.Summary.cshtml) some wierd behavior  collection with two behavior objects is returned and exploring them doesn't seem to get me any closer to getting the values.

I'm sure I'm missing something really obvious??

Thanks!

Coordinator
Apr 19, 2012 at 5:52 PM

In any view, Model represents the shape itself (a dynamic object). So when you are in the taxonomy field template, that's the shape for the field, and it has the collection of terms. When you are in content summary, Model is a shape that represents mainly the available local zones, but it also has a ContentItem property that enables you to drill for any part or field (shape tracing will show you that if you focus it on the right shape). This article discusses what's available from this exact template, among other things: http://weblogs.asp.net/bleroy/archive/2011/07/31/so-you-don-t-want-to-use-placement-info.aspx

So it should be something like @Model.ContentItem.ProjectFile.Department.Terms[0].Name

Apr 19, 2012 at 6:48 PM

Thanks for the reply!

@Model.ContentItem.ProjectComment.Department.Terms[0].Name;

cause this error

Cannot apply indexing with [] to an expression of type 'Orchard.ContentManagement.Utilities.LazyField<System.Collections.Generic.IEnumerable<Contrib.Taxonomies.Models.TermPart>>'

I have tried several different variations without success. thx!

Coordinator
Apr 19, 2012 at 6:59 PM

Fine, add Value before the indexing. It's also possible you may have to cast that intermediate result too.

Sep 10, 2013 at 6:40 PM
Edited Sep 10, 2013 at 6:42 PM
For those that may be coming to the page more recently (for Orchard 1.7) here is the way I displayed taxonomy terms in a template (.cshtml) for a custom content type. So, I have a content type called Events and a taxonomy called Categories. In my Content-Events.cshtml template I displayed the terms assigned to my Events item like this:
@foreach(var term in Model.ContentItem.Events.Categories.Terms) {
    @term.Name
}
The syntax is a little different than the example above but this is the one that worked for me.