Controller rendering in Sitecore MVC with a full example

- Pranay on Jan 20 '15
- viewed 16946 times

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.

  1. Assuming that you have already created and configured a .NET MVC project for sitecore, create a new controller in that .NET MVC solution.
  2. Name the controller as ‘Cart’ and create a new action method ‘List’ inside it as shown below.

    CartController.cs

    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
        }
    }
    
  3. 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.

    List.cshtml

    @model Sitecore.MyMVCSolution.Website.Models.CartList
    @{
        if(Model!=null)
        {
            foreach(item in Model.CList)
            {
                <div class="testrow">
                    <div class="prod-name-css">@item.ProductName</div>
                    <div class="prod-qty-css">@item.ProductQty</div>
                </div>
            }
        }
        else
        {
            <div>
                <h4 style="text-align:center;">
                    There are no items in this cart. 
                </h4>
            </div>
        }
    }
    

Create Controller rendering in sitecore

  1. Login to Sitecore Content editor. Go to Layouts->Renderings.
  2. Right click on Renderings, click on Insert->Controller Rendering. Enter the data values as shown below.

    Controller: Cart, Controller Action: List

  3. Save and publish the item.

Create sitecore Content Item

  1. 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.
  2. Create another item under the newly created item. Name it as the action method in the Controller i.e., ‘List’.
  3. 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.

  1. Click on the ‘List’ content item in sitecore.
  2. Go to ‘Presentation’ tab and click on ‘Details’.
  3. Edit the default details by clicking on ‘Edit’ link.
  4. Under Layouts, select the layout you want to choose.

    Sample Layout File with placeholders (SampleLayout.cshtml):

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Sample Page</title>
        </head>
        <body>
            <div class="header">
                @Html.Sitecore().Placeholder("phHeader")
            </div>
    
            <div class="Body">
                @Html.Sitecore().Placeholder("phCartListController")
    
            </div>
    
            <div class="footer">
                @Html.Sitecore().Placeholder("phFooter")
            </div>
        </body>
    </html>
    
  5. 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.
  6. 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.

About the author

Pranay Deegoju
Sitecore Certified Professional

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.



Leave your comments on this post here


SIGN UP AND GET LATEST UPDATES.

Recent Posts

×

Liked this article?

Subscribe with your email and get updates when ever a new article is posted.

By subscribing you agree to our privacy terms. We promise that we do not sell your data.