Error while retrieving Categories from XML in Tridion (DD4T - MVC)

433 views Asked by At

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.

5

There are 5 answers

2
Quirijn On BEST ANSWER

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.

0
Chris Summers On

Have you published your categories to your target?

0
Arjen Stobbe On

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.

0
vikas kumar On

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

Thanks, Vikas Kumar

1
Neil On

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.