Specifying the MVC Area For Renderings

When using MVC renderings, Sitecore does not provide a specific way to designate the MVC area that the rendering should exist within. This will provide the developer a way to specify the MVC area name within the content tree when defining the rendering definition items.

Why?

The MVC Area is used to tell MVC the location of assets within the running website. In SCORE, we recommend the use of MVC areas to isolate assets within each tenant of a multi-tenant environment.

Controller rendering view resolution

When creating controller renderings, MVC will "expect" that the view is located in a specific place (based on how the view is referenced by the controller).

example:

Sample Controller Rendering
public ActionResult SomeActionName()
{
   Item page = PageContext.Current.Item;

   var model = new SomeActionModel();
   model.Initialize(RenderingContext.Current.Rendering);

   NavigableItem menuRoot = GetSpiderMenuRoot();
   if (menuRoot == null)
   {
   		return Sitecore.Context.PageMode.IsPageEditorEditing
                    ? (ActionResult)PartialView(model)
                    : new EmptyResult();
   }

   IList<Item> sectionList = GetChildrenMenuItems(menuRoot).ToList();
   model.DropListMenuItem = new DropListMenu(menuRoot, sectionList); 
                
   // return the partial view and attach the model
   return PartialView(model);  
}

The statement return PartialView(model) will cause .NET to look for a view in the views directory called ControllerName/SomeActionName.cshtml.  By default, MVC will look in the root /Views folder, and within the current area. 

 DisplayTemplates and EditTemplates

Display and Edit Templates can be used to modularize the UI code to small cshtml files that are designed to render specific model classes.  This can be used instead of formatting your output with helpers or worse with code snippets that are used as HTML formatters.

example:

@model Score.BootstrapUI.Web.Areas.ScoreBootstrapUI.Models.Component.Navigation.Breadcrumb
@if (Model.IsActive)
{
    <li class="active">@Html.Sitecore().Field("Breadcrumb Title", Model.Item)</li>
}
else
{
    <li>
        <a href="@Sitecore.Links.LinkManager.GetItemUrl(Model.Item)">@Html.Sitecore().Field("Breadcrumb Title", Model.Item)</a>
    </li>
}

the model class behind this display template is:

using Sitecore.Data.Items;

namespace Score.BootstrapUI.Web.Areas.ScoreBootstrapUI.Models.Component.Navigation
{    
    public class Breadcrumb
    {
        public Item Item { get; set; }
        public bool IsActive { get; set; }

        public Breadcrumb(Item item, bool isActive)
        {
            Item = item;
            IsActive = isActive;
        }
    }
}