Search code examples
razorsitecoreglass-mappersitecore-mvcsitecore8

Sitecore MVC ViewRendereing and GlassMapper


I would like to create a breadcrumb based on Razor on Sitecore 8 MVC. I created a page template with some property that would be use for it. View rendering was created and added to layout item (page). I have created a cshtml file for this rendering with while loop

 <ul>
    @{
        var parentItem = Html.Sitecore().CurrentItem;
        while (parentItem != null)
        {
            if (parentItem["Include in breadcrumb"] != "0")
            {
                <li>@parentItem["Short Title"]</li>
            }
            parentItem = parentItem.Parent;
        }
    }        
</ul>  

But it is looks not very optimal for me, I would like to use Glass Mapper for it

I created a class with ancestor access

public class Breadcrumb
{
    [SitecoreQuery("ancestor-or-self::*[@#Include in breadcrumb#='1']", IsRelative = true)]
    public virtual IEnumerable<Breadcrumb> Ancestors { get; set; }

    [SitecoreField(FieldName = "Include in breadcrumb")]
    public virtual bool IncludeIn { get; set; }

    [SitecoreField(FieldName = "Short Title")]
    public virtual string Title { get; set; }
}

but I don't know how to 'say' to sitecore use it for my View rendering. I don't have any specific controller for page.

Is it possible to use some how glass mapper with View rendering ?


Solution

  • This usually is done via inherits directive at the top of your view:

    @using Glass.Mapper.Sc.Web.Mvc
    @inherits GlassView<Breadcrumb>
    

    Then on page you may use your model as normal; if, for a case, you want to have your title Page Editor friendly - then you may use the following construction to access your model properties:

    @Editable(x=>x.Title)