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