4

In a DD4T View I am trying to pick the value of Path of the keyword inside the Category.

foreach(var category in @Model.Categories)
{
    if (category.Title.Contains("Taxonomy"))
    {
        str = category.Keywords[0].Path;            

        break;
    }
}

but getting null in @Model.Categories.

Error: Object reference not set to instance of the object.

Although data exist in XML.

Please suggest.

tereško
  • 58,060
  • 25
  • 98
  • 150
Meenakshi
  • 599
  • 3
  • 13

5 Answers5

7

I discovered this is an issue in DD4T. The work-around is quite simple: if you use the implementation of Component (or Page) as your model, rather than the interface, it works.

So start your view with:

@model DD4T.ContentModel.Component

Rather than

@model DD4T.ContentModel.IComponent

And try again.

Quirijn
  • 3,549
  • 15
  • 30
  • Hi Quirijn, If you give us some more information on this, we can look in to code to fix this. – vikas kumar Jun 20 '12 at 05:35
  • @Vikas: I created an issue on the DD4T google code site: http://code.google.com/p/dynamic-delivery-4-tridion/issues/detail?id=31. If you could see your way to fix it, that would be awesome. – Quirijn Jun 20 '12 at 07:57
4

I have logged this as an issue in the DD4T Google Code site here.

It seems this is caused by contravariance not being supported by List and IList, meaning that lines like:

IList<ICategory> IComponent.Categories
{
    get { return Categories as IList<ICategory>; }
}

in the ContentModel class will never work. The suggestion from digging around is to change this to IEnumerable which does support contravariance.

Community
  • 1
  • 1
Neil
  • 2,688
  • 1
  • 23
  • 32
  • Thanks Neil, that sounds like it might be it, indeed. Great word, contravariance, I'm going to be using it in casual conversation :) – Quirijn Jun 21 '12 at 13:36
3

Its working after implementing Quirijn suggestion like Component c = (Component)Model; c.Categories[0]...

Thanks, Vikas Kumar

vikas kumar
  • 2,444
  • 15
  • 25
2

Have you published your categories to your target?

Chris Summers
  • 10,153
  • 1
  • 21
  • 46
0

Yes, verify first if you published the Categories to the Broker Database. The way Page or Component XML is deserialized into an IPage or IComponent object is pretty straight forward.

Also indicate which version of DD4T you're using. I remember there was an issue with deserialization in earlier releases.

Arjen Stobbe
  • 1,684
  • 9
  • 15