This post explains how to create a controller rendering and related items like layout, templates, presentation details in Sitecore MVC with a simple example.
Create MVC Controller and action method in .NET MVC Solution.
- Assuming that you have already created and configured a .NET MVC project for sitecore, create a new controller in that .NET MVC solution.
Name the controller as ‘Cart’ and create a new action method ‘List’ inside it as shown below.
public class CartController : Controller
public ActionResult List()
CartList model = new CartList();
//model = ………..; Perform database operations and retrieve model details
return View(model); //return model details to the view
Create a corresponding view for this action method. i.e., create List.cshtml under Views/Cart/ path in the same .NET solution. Sample view file is shown below.
foreach(item in Model.CList)
There are no items in this cart.
Create Controller rendering in sitecore
- Login to Sitecore Content editor. Go to Layouts->Renderings.
Right click on Renderings, click on Insert->Controller Rendering. Enter the data values as shown below.
Controller Action: List
Save and publish the item.
Create sitecore Content Item
- In Sitecore content editor, create a new item under Content. Name this as the controller name i.e., ‘Cart’. For creating this, select any default template or create a new one based on your need.
- Create another item under the newly created item. Name it as the action method in the Controller i.e., ‘List’.
- Once these two items are created, the path ‘http://Website/Cart/List’ will be available. Whenever this link is called from a browser, the request will be hitting the List content item. Now we need to make sure that it is rendered appropriately using a Controller rendering.
Configuring presentation details of sitecore content items.
- Click on the ‘List’ content item in sitecore.
- Go to ‘Presentation’ tab and click on ‘Details’.
- Edit the default details by clicking on ‘Edit’ link.
Under Layouts, select the layout you want to choose.
Sample Layout File with placeholders (SampleLayout.cshtml):
Under Controls, add the rendering items for this page. For each placeholder in the layout file add a presentation/rendering item. Here the rendering item could be ‘view rendering’, ‘controller rendering’, ‘XSLT rendering’ or of any other type based on our need.
- For adding the controller rendering, click on ‘add’.
- Under Renderings in the left, select the controller rendering we have created i.e., ‘CartListControllerRendering’ and select the placeholder as per the layout file. Say ‘phCartListController’. Click on ‘select’ and then ‘ok’.
- Save and publish item.
Now run the URL ‘http://Website/Cart/List’ from the browser. This will hit the ‘List’ content item in the sitecore. While rendering the ‘List’ item, the controller action is called. The controller action will return a model to the view. The view is rendered and added to the placeholder ‘phCartListController’ in the layout file.