1

In learning DD4T, I have made reference to Albert Romkes' article DD4T Continued: rendering component presentations where he gives an example using TridionControllerBase.

So far, most of the answers to my question How do I make use of DD4T's provided ViewModels? have used examples with ModelControllerBase

What is the difference between these techniques, and when should I use one and when the other?

1 Answer 1

4

The biggest difference is the inclusion of IViewModelFactory. This allows ModelControllerBase to return the Model converted to your associated View Model type. TridionControllerBase returns the Model as a base Tridion type e.g. Page and ComponentPresentation.

Compare for example:

TridionControllerBase

[HandleError]
public virtual ActionResult Page(string url)
{
      url = AddWelcomePageToUrl(url);
      IPage page = GetPage(url);
      if (page == null) { throw new HttpException(404, "Page cannot be found"); }
      return View(GetViewName(page), page);
}

ModelControllerBase

[HandleError]
public virtual ActionResult PageModel(string url)
{
      url = AddWelcomePageToUrl(url);
      IPage page = GetPage(url);
      if (page == null) { throw new HttpException(404, "Page cannot be found"); }
      IViewModel pageViewModel = ViewModelFactory.BuildViewModel(page);
      return View(GetViewName(page), pageViewModel);
}
1
  • 1
    As an addition to Chris' answer, which explained the main difference: in real world implementations, you don't need to use any of them. Depending on the requirements, you can also create your own controllers and just reuse the ideas from TridionControllerBase and ModelControllerBase. Commented May 24, 2016 at 7:03

Your Answer

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

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