MVC and renderings

Last updated Tuesday, May 30, 2017 in Sitecore Experience Platform for Developer
Keywords: Development

Sitecore MVC uses a different set of controls than ASP.NET WebForms, and it therefore does not support all Sitecore renderings.

These two supported types of rendering are the most commonly:

  • View Rendering

    This rendering type is a Razor view. Sitecore provides a default controller and model for the view. You can customize the model by changing the mvc.getModel pipeline.

    Use this when you want to render item content that does not require any significant business or presentation logic.

  • Controller Rendering

    For this rendering type, you supply controller, model and view. You specify in the rendering definition item what action Sitecore takes to render the component.

    Use this to render content that relies on external data and/or requires significant business or presentation logic.

This rendering is used occasionally:

  • Item Rendering

    This rendering type requests that content (an item) renders itself. The content has information about how it renders.

The following renderings are supported for legacy reasons:

  • Method Rendering

    This rendering type outputs the return value of a call to a static method.

  • XSLT Rendering

    Use this to render Sitecore XSLT on a Sitecore MVC page. This rendering type is similar to the View Rendering type, but using XSLT as the template engine instead of Razor.

    Use this if you have a library of existing Sitecore XSLT components that you do not want to rewrite.

  • Url Rendering

    This rendering type renders the response of a HTTP GET request onto the current page.

    You can use it if you need to screen scrape HTML from another system. Can sometimes be used to avoid using iframes.

Note

The renderer classes in Sitecore MVC do not inherit from WebControl but from Renderer.

Adding layouts and renderings to an MVC solution

You can add MVC layouts and renderings to an MVC solution in two ways: either from Sitecore Rocks or from the Content Editor.

To add an MVC layout from Sitecore Rocks:

  1. Right-click the Views folder in the Solution Explorer in Visual Studio, and select Add, New Item…
  2. Select Sitecore View Layout in the Siteccore/MVC group:

    Picture 1

  3. Sitecore Rocks will ask where to create the corresponding Sitecore definition item. Put the item in the Layouts folder in the master database:

    Picture 2

You create renderings in the same way:

  • Choose Sitecore View Rendering to create a View Rendering, and put the definition item in Renderings.
  • Choose Sitecore Controller Rendering to create a Controller Rendering, and put the definition item in Renderings.

To add an MVC layout from the Sitecore Content Editor:

  1. Click the Layout folder in the content tree.

    Picture 4

  2. Click MVC Layout to create the definition item:

    Picture 5

  3. Enter a name and click next. Place the item in the Layouts folder in Sitecore or a subfolder under Layouts:

    Picture 6

  4. Sitecore generates the cshtml file that corresponds to the definition item. Place it in the Views folder of your Visual Studio project:

    Picture 7

Note that Sitecore places the file in the webroot file structure - not in your Visual Studio project structure.

You create any of the renderings mentioned by clicking the Renderings folder in the content tree:

Picture 8

Send feedback about the documentation to docsite@sitecore.net.