Sitecore MVC

Glass.Mapper can generate the models used by Sitecore MVC. Models used by Sitecore MVC View Renderings are generated by the mvc.getModel pipeline.

To get started with Glass.Mapper.Sc and Sitecore MVC you will need to download the Glass.Mapper.Sc.CastleWindsor project and also the Glass.Mapper.Sc.Mvc project that is specific to the MVC version you are using, for example if you are using MVC4 you need to download Glass.Mapper.Sc.Mvc-4.

When you install Glass.Mapper.Sc.Mvc a config file called Glass.Mapper.Sc.Mvc.config is inserted into the /App_Config/Include directory, this config file adds an additional processor to the mvc.getModel pipeline to create models from Glass.

For this tutorial I have assumed you have already setup Sitecore to use MVC. We are going to create a new device that allows us to output news articles as XML so that other services can consume them.

In Sitecore I have defined the following ViewRendering:


I have specified the NewsLanding model created in Tutorial 7. The mark-up for the CSHTML page is very simple:

@inherits System.Web.Mvc.WebViewPage<Glass.Mapper.Sites.Sc.Models.Landing.NewsLanding>
<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
    <title>@Model.Title</title>
    <articles count="@Model.Articles.Count()">
        @foreach (var article in Model.Articles)
        {
            <article>
                <title>@article.Title</title>
                <image>@article.FeaturedImage.Src</image>
                <content>@article.MainBody</content>
                <date>@article.Date</date>
            </article>
        }
    </articles>
</news>

In this example we are inheriting from the standard System.Web.Mvc.WebViewPage but you can also inherit from Glass.Mapper.Sc.Web.Mvc.GlassView which will give you the ability to make fields page editable.

If we navigate to the news landing page setting the device to XML we get a very simple XML rendering of our news items:

<?xml version="1.0" encoding="iso-8859-1"?>
<news>
    <title>News</title>
    <articles count="3">
            <article>
                <title>New Features Released</title>
                <image>/~/media/Site/boots.ashx</image>
                <content></content>
                <date>07/12/2012 00:00:00</date>
            </article>
            <article>
                <title>Development time cut to reduce cost</title>
                <image>/~/media/Site/catonsofa.ashx</image>
                <content><p>Bacon ipsum dolor sit amet doner shoulder flank, filet mignon tail biltong 
                    turkey sausage fatback jerky boudin. Swine beef bresaola ball tip sausage, kielbasa 
                    fatback leberkas. Pork drumstick venison short loin jerky andouille frankfurter tail 
                    ham rump boudin. Leberkas pig short loin, sausage biltong spare ribs ribeye bresaola salami 
                    </p></content>
                <date>18/02/2013 00:00:00</date>
            </article>
            <article>
                <title>Glass Mappers Official Release</title>
                <image>/~/media/Site/snowboard.ashx</image>
                <content></content>
                <date>01/02/2013 00:00:00</date>
            </article>
    </articles>
</news>

You can see from this tutorial that we don't actually have to do anything special to get our Glass.Mapper models working with Sitecore MVC.