Navigation and service panel


The Sitecore MVC puzzle

By Kevin Brechb├╝hl on 24. June 2015, 1 Comment

I got the chance to hold a presentation at the Sitecore User Group Conference Europe this year in Eindhoven (NL). My topic was "Sitecore MVC Advanced". This blog post should give you a recap of my session. You can also get the slides and there is also a recording of my session available.

Sitecore MVC is a bit different from traditional MVC. The concept and the idea remains the same, but there are several pitfalls you need to know while working with Sitecore MVC. For my session at the SUGCON 2015 I took the main issues we had in recent projects and put them together to a puzzle. The goal of my presentation was to give an overview of the different puzzle pieces available and show how you can integrate them into your solutions. The following paragraphs gives you an overview of the different puzzle pieces.

Sitecore MVC vs. MVC

In MVC a request always correspond to a MVC route. This has one controller which is being instantiated and one action which is being invoked. In Sitecore MVC a request correspond to a Sitecore item (usually) where the layout is defined in the presentation details of the item. Focusing on Sitecore Controller Renderings, this means that we can have multiple controllers during one request, where multiple actions are invoked.

Multiple forms

You can always have multiple forms on a page (e.g. a login form in the middle of the content and a search box in the top right corner). In MVC each form is posted to a specific controller/action. In Sitecore MVC you usually post to the same url (as you want to return the same item again). The result of this is, that each [HttpPost]-action on every of your controller is executed (and not only the one for the form you have submitted).

There are several solutions available to solve this problem:

  • Post the form via Ajax to a specific controller of your choice
  • Use the Sitecore Form handler to post your form to a specific controller
  • Use a custom ├ÇctionMethodSelector

For the first two we need to consider what to do with validation on the server side. I like the third option the most, as it's very close to traditional MVC. I wrote a blog post about this here.

MVC Forms localization

When you don't use Web Forms for Marketers for building your forms, you need to translate the input field labels and the validation messages somehow programmatically. Usually in this can be done with resource files, but in Sitecore we want to use Sitecore Dictionaries for this. There are two things you need to implement for making this possible:

  • Write a custom MetaDataProvider to translate the labels (see here)
  • Write a DataAnnotationsModelValidator for each available validator (see here)

With this simple implementations you can pass the dictionary key to the attributes on your view models and the rest is handled for you :)

See the full code of this on GitHub.

MVC Areas

The third piece is about organizing your code. When you have multiple sites with different layouts in one Sitecore installation (e.g. a corporate site and closed user group) then you can split all the controllers, models and views into separate areas. Sitecore has no support for areas out of the box, but there are multiple solutions available for integrate them in your solutions:

  • Resolve the area in the mvc.renderRendering-pipeline (see here)
  • Use a custom ControllerRunner and a custom Renderer (see here)
  • Resolve the area by the <site> configurations (see here)

We also saw that Sitecore is working on a solution to integrate areas into the core (see here). Rumor has it that they will integrate a similar pattern as BrainJocks does with the mvc.renderRendering pipeline.


This is just a small summary of my presentation. If you want to see the full session with audio you can see it on YouTube. If you only want to see the slides, you can get them on SlideShare. The sample code is available on GitHub.

Hope that I could give you an overview of the Sitecore MVC puzzle and you can start to enrich the puzzle with your custom pieces in your custom Sitecore solutions. Again I want to say a big THANK YOU to the organisation of the SUGCON and to all the sponsors (including Unic), it was a great event. All of you who couldn't attend this year, stay tuned and join the next year.

Categories  ASP.NET MVC  Events Tags  sugcon  session  presentation

1 Comment

  1. David Mead
    24. June 2015 at 15:10

    I like this quick summary. Thanks for it.

Add your comment

Your email address will not be published. Required fields are marked *