7

I am looking for a way to render a Component which is published, but not part of the page (IPage object) that DD4T renders.

What I tried:

@Html.Action("GetView","Component",new ComponentPresentation(){
       Component = new Component(){
           Id = "tcm:180-278512"
       }
})

Of course it doesn't work, any help is welcome.

9

Assuming your dynamically published Component Presentation is using a default DD4T style Component Template, all you need to do is retrieve the Component using the ComponentFactory as part of your controller action.

You can get the ComponentFactory from the DependencyResolver like so

var componentFactory = DependencyResolver.Current.GetService<IComponentFactory>();

and then get the Component like this

IComponent component = componentFactory.GetComponent("tcm:180-278512");

Optionally you can put a Component Template ID into the GetComponent method as a second parameter - this is important if you have multiple DCPs with different link levels set on the CT.

If you're using something like Ninject as your dependency injection container to construct your controllers you could add the ComponentFactory as a property or constructor parameter to make you code more flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.