In this tutorial, you will learn the differences between view renderings and controller renderings within Sitecore CMS. When learning Sitecore, it can be very overwhelming. You will be bombarded with lots of new terms and things to learn. One of the most fundamental aspects of creating a website is being able to add components to a page. Being able to re-use HTML fragments is an essential step within a project. On a Sitecore project, when we talk about HTML components you will need to create renderings within the CMS. Within Sitecore, you can create two different types of renderings. This raises the question, what are they and in which situations does it make sense to use one over the other? In this guide, you will learn the answer to that question, do if you want to master Sitecore CMS, read on 🔥🔥🔥

View Renderings

A view rendering is an MVC view. A view rendering is built from a single cshtml file and nothing else. In terms of usage, a view rendering can be compared to a partial view within a vanilla MVC website. You can pass a Sitecore RenderingModel object into the view in order to render CMS data. A RenderingModel can be defined just like any normal POCO or C# class. As a developer, you are free to define your own properties.

The model will get injected into the view via Sitecore on-page request. The biggest advantage of using a view rendering is the ease of use. From my experience, I would say you would usually want to use a view rendering when you want to display some HTML that has no logic in it at all. If you have a need to output some data from a context item, or data source item a view rendering will do the trick 😊

One example of where you might use a view rendering is when you need to render the same data in different ways. Another could be a product on an e-commerce site. The product uses the same data, like Name, Description, Price, however, depending on where the product is being rendered to the site may affect how it appears. On a PLP page, the product may render very differently compared to the PDP page. In this example, you could create one ProductModel and then create many view renderings to display it differently.

Controller Renderings

A controller rendering will be familiar to developers who come from a standard ASP.NET MVC background. When we define a controller rendering in Sitecore, instead of specifying a view, we point Sitecore to an action method within a custom controller that you have created. When a controller rendering is added inside of a Sitecore placeholder and a request is made to render the control, the controller and action you define within Sitecore will be executed. Within this action method, you can add your custom business logic, generate a view model and call a view

Controller renderings should be used when you need to add business logic into your control. Maybe you need to call a web API in your controller to get some real-time data, or, you want to dynamically get different content from Sitecore, like within a search results component. When you use controller renderings it also means you can unit test your work and use dependency injection. If you combine this with a good deployment process, you can get better confidence that the changes you have made won't break your system.

View Renderings Vs Controller Renderings

In terms of which one is better, I'm afraid this is where I give you a cop-out answer, I can't answer you. As all sites are custom, there is definitely no definitive right or wrong answer. I tend to favour controller rendering, as they allow me to unit test my work. If you need to do any business logic in your rendering, you will want to do that in a controller rather than a view. As in life, use the technique that makes you happy 😊

I hope this has cleared up any confusion you might have between view renderings and controller rendering. The basic concept is that view renderings are very simple and should be used when simply rendering data as-is from the CMS. Controller renderings are more advanced and should be used when you need to do some extyra processing before the control is rendered. Happy Coding 🤘