7

I have following requirements for DD4T.

  1. I want to write custom business logic for my components.
  2. I don't want to write business logic in my component views, as it will violate the mvc principles
  3. I want to write Unit Tests for my component.

This is what I thought for

  1. Create a Custom Controller and Create an Action to represent particular component and build the model. Then return the model to the view.

    public class ArticleViewController:TridionControllerBase {
        // GET: ArticleView
        public ActionResult ArticleViewCT(IComponentPresentation componentPresentation)
        {
            var model = ComponentViewModelBuilder.Build<ArticleViewModel>(componentPresentation.Component); //or Some Extra database calls
            return View(model);
        }
    }
    

then in Page template I want to call this Action

@{Html.RenderAction("ArticleViewCT", "ArticleView");};

but it's throwing an error

[MissingMethodException: Cannot create an instance of an interface. Object type 'DD4T.ContentModel.IComponentPresentation'.]

Can you suggest me to implement, which helps to solve my problems. I love to work as normal MVC works. I am also using this package to create viewModels ViewModel for DD4t

1
  • If you're looking for a much more robust solution, check out DVM4T. Here's an introductory blog post I wrote: DOMAIN VIEW MODELS FOR TRIDION -- let me know if this looks like something you might want to use and I'd be glad to help out! Full disclosure: that's my project and blog post. Mar 16, 2015 at 16:26

2 Answers 2

6

You need to use Component Template metadata to tell DD4T the name of your controller (ArticleView) and action (ArticleViewCT) to call when rendering:

enter image description here

You can remove the RenderAction call, DD4T will do this for you in essence when it discovers the overrides.

The component template metadata should look something like:

enter image description here

6
  • I am not able to see this option. Can you tell me how we can enter these options in component template.
    – Ravi Gadag
    Nov 21, 2014 at 9:40
  • Have you uploaded the DD4T TBBs into Tridion as per the quickstart (code.google.com/p/dynamic-delivery-4-tridion/wiki/…)? Essentially you need a metadata schema with fields, view, region, controller and action.
    – Neil
    Nov 21, 2014 at 9:44
  • I have installed it. I am using my own metadata to set the view name in CMS. is there any thing i am missing
    – Ravi Gadag
    Nov 21, 2014 at 9:46
  • Edited my answer.
    – Neil
    Nov 21, 2014 at 9:49
  • No updates. still it's same. did u edited the answer ?
    – Ravi Gadag
    Nov 21, 2014 at 9:50
3

I think your problem is of a very different nature:

then in Page template I want to call this Action @{Html.RenderAction("ArticleViewCT", "ArticleView");};

In the Tridion implementation model you don't hardcode the call to a CT from a PT; you let the PT render the component presentations that are on the page via

@Html.RenderComponentPresentations()

This will trigger DD4T to render the underlying component presentations using the CTs as configured on the page (as opposed to the page template!).

If you want to execute the CT action from the page you would need to provide it with a valid model (which you do not in your code) but doing so makes no sense from a Tridion perspective (it is not the responsibility of the page template to know what content is on the page!).

If you want to use a specialized controller for the component presentations you will need to configure the component template metadata as indicated by Neil; this will cause the DefaultComponentPresentationRenderer in DD4T to use the given controller, action and view...

1
  • yup. I followed niel suggestion :) thank you for detailed answer :)
    – Ravi Gadag
    Nov 21, 2014 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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