Glass.Mapper Page Editor Support for Children & Items using MVC

Typically, using Glass.Mapper with MVC you can make a field editable by calling the Model that was passed to the View as follows:

@using Glass.Mapper.Sc;
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<SitecoreSandbox.Models.NavigationViewModel>

<h1>@editable(x => x.Title)</h1>

But what about if you need to make an field editable that is not in the Model such as a Site Settings item field? Glass.Mapper has an overloaded method to pass in any model. Take a look at how I get my Site Settings item by using the “siteModel” variable after it is set to the item as a “Site” model (model generated by TDS code generation):

@using Glass.Mapper.Sc;
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<SitecoreSandbox.Models.NavigationViewModel>
@{ var siteModel = new SitecoreContext().GetItem<SitecoreSandbox.Models.Site.Site>(SitecoreSandbox.Helpers.Configuration.GetSettingsItem().ID.Guid); }

<a href="/">
     @RenderImage(siteMdoel, x => x.Site_Logo)

Also, you can make the children of an item editable by using a BeginEditFrame method in Glass.Mapper. Again, only the Model.Children are available to the View via the passed in Model (NavigationViewModel which was created in another post where I talk about using ViewModels), but I want the children to be editable to my Content Editors. I can accomplish this as follows:

@using Glass.Mapper.Sc; 
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<SitecoreSandbox.Models.NavigationViewModel> 

    @foreach (SitecoreSandbox.Models.Site.Navigation.Navigation_Item navigationItem in Model.Children)                 
        using (BeginEditFrame(navigationItem.Url))                     
               @(RenderLink(navigationItem, x => x.Link, isEditable: true))

Glass.Mapper and TDS Code Generation are a great tool for your toolbox, and here is just a bit more knowledge to pass along when using Glass.Mapper and MVC to ensure Page Editor support. Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s