Use item wrappers as models in Sitecore MVC

Posted by Robin Hermanussen on March 04, 2013 · 4 mins read

I’ve been experimenting with Sitecore 6.6 MVC lately. I love the very clean Razor views. And if you’re used to working with a rich domain model that wraps your Sitecore items, then you’ll hardly ever need a controller rendering (because your view-logic will be on your model). Most of the layouts and sublayouts will be view renderings.

Now, when developing in ASP.NET Web forms, I will usually have a base class for my controls that has a generic type parameter that reflects what type of item it will take as a datasource. This way, I can easily access the right fields in a strong-typed fashion. And I wanted this same setup in MVC.

So, after completing the steps in this blog post, you will be able to make a Razor view like this:

Line-by-line explanation:

  1. Declare the model to be a IRenderingModel-derived type (this way, it will be initialized in the same way as the default rendering model). The generic type parameter shows that this particular view will take any item that has the “Page”-template or a template that inherits from it as its datasource.
  2. Use an extension to the SitecoreHelper to reference the “Title” field. It uses a lambda expression, so that the helper method can ensure that the “renderField” pipeline is run; this ensures that among other things, the page-editor support will be preserved.
  3. The same as #2, but for the “Text content” field.

Side-note: I use my own CompiledDomainModel module (CDM) to generate wrapper classes based on Sitecore templates. So the following example code will use it as well. But you can probably easily adapt the following code to work with your mapping/wrapping framework. The code is from a project that I will soon use in a presentation for the SUGNL (hence, the UnitTestingDemo namespace).

The following code is for the RenderingModel. It allows access to the datasource itemwrapper in the type you want. Notice that the “out” keyword is used before the generic type parameter declaration. This means that the type is covariant, so that you can use a base type in your model declaration at the top of the Razor view (e.g. IRenderingModel instead of IRenderingModel).

To create the RenderingModel, we need to plug the following in to the “mvc.GetModel” pipeline.

Hook this code up to the “mvc.GetModel” pipeline using the following configuration (use a config file in the “/App_Config/Include” folder to make a nice patch of this):

Now, when you make a rendering, be sure to add “typedmodel=1” to the parameters field in Sitecore for that rendering. That way, the “mvc.GetModel” pipeline will know when to create a typed model.

And you’re all set to use the typed model in your Razor views. But there’s one thing left; the lambda syntax support. This works in the same way as the CdmFieldRenderer control from a previous blog post. You can use the following helper class:

To be able to use this extension without having to import it every time, you can add it to the “/Views/Web.config” file, like this:

I know it takes a little time to setup, but it makes your Razor views very clean and you’ll have code-completion for your field names. If you want, you can compile your Razor views at build-time by editing your MVC project file and setting “true”.

Next week, I’ll be doing a presentation on unit testing with test fixtures and I’ll also get into unit testing the Razor view that I mentioned here. The whole project and the slides will be available for download on this blog afterwards.