Mixing content and functionality with DD4T

Today I received a question from a developer who is just starting on DD4T. He wants to have a page which consists of regular Tridion content but also contains a form. Being a .NET MVC developer he naturally wants to handle this form with a controller and various  actions to handle GET and POST and perform validation as well as sending an email on successful submission.

I call this type of web page, which combines plain content with application logic, a ‘mixed’ or ‘hybrid’ page. DD4T was designed to handle such pages (as well as the more simple content pages). To understand how DD4T does this, you need to know that the framework uses controllers and actions on two different levels:

  • The level of the page (just like in any MVC app)
  • The level of the component presentation

Let’s start by looking at a very simple page, which does NOT contain any forms or other application logic:

 

As you see, there are two views involved to build this rather simple page. The controller and action are predefined by the framework – unless you override them, which I will explain later. The controller/action for the page retrieves the page from the Tridion broker and uses that as its model. The controller/action for the component uses the component as its model. All YOU need to do is write the views.

You may wonder how DD4T knows which views to call. This is achieved by configuring the name of the view in the metadata of the page template and component template.

Now I’m coming back to the question of mixed pages. A mixed page has application logic as well as plain content. To handle application logic in MVC, you need a controller and an action. For example: you might want to display a form when the method is GET, and validate it when the method is POST.

In a diagram, this is how it will look:

The page now contains two component presentations: one that represents (and contains) just plain content and another one that represents the form. The neat thing is that your custom action will automatically be called by DD4T. All you need to do is configure your custom controller and action using metadata on the component template. So instead of just specifying a view, you will now have to specify a controller and action as well.

Advantages of this ‘mixed page’ approach:

  • Tridion stays in control of the URL
  • Editors can decide where to put a form, and which other pieces of content to put next to it
  • The placement of the form and other pieces of content is handled by the exact same view as on normal pages, so you do not have to do extra coding.
  • All MVC functionality like model binding and validation are fully supported

Happy coding!

 

5 thoughts on “Mixing content and functionality with DD4T

  1. If you can provide an example for this that will be of great help. I am trying to incrporate custom controller in DD4T but somehow it is not being called by the DefaultComponentPresentationRenderer()

  2. First, you need to create the metadata fields ‘controller’ and ‘action’ in the metadata schema for your component templates. Then edit your component template and specify the names of your controller and action. Save the CT and republish it to view the result.

  3. A common problem here is you create a view to represent your form, “Index” and a controller to handle it with actions, “ContactUsController” – in your form you might use Html.BeginForm(“Post”, “Contact”) to handle the submitting – but this doesn’t immediately work because the action of the form is “/Contact/Post” this likely wont match a published page.

  4. Is it possible to create these without adding the metadata? I see it’s possible to obtain the view name without using the meta (taken from the component template title).

  5. You are correct, if you want to override the default action for component presentations, you must have a metadata schema attached to the component template with the following fields:

    • Controller
    • Action
    • View (optional, but recommended)

Comments are closed.