There will be times where you want to enable the Page Editor for a property on your model but the model itself may not have been mapped by Glass.Mapper.

The most obvious scenario for this is when using Sitecore search, you get search results back that are populated by Sitecore 7 search. The properties are filled with data from the Sitecore 7 search service but now you need to make them editable.

Lets look at the raw search code first, my model contains an ID property and a Title property, they will map the item's ID and Title field from the index respectively.

        public class MyResult 
        {
            [TypeConverter(typeof(IndexFieldIDValueConverter))]
            [IndexField("_group")]
            public ID Id { get; set; }
            
            [IndexField("title")]
            public string Title { get; set; }
        }

The code behind on my search ascx is shown below, notice that the results are coming directly from the search and are not going via Glass.Mapper, the user control also inherits from the AbstractGlassUserControl, this is just for convenience later.

    public partial class Search : AbstractGlassUserControl
    {

        public IEnumerable<MyResult>  Results { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            var index = ContentSearchManager.GetIndex("sitecore_master_index");
            using (var context = index.CreateSearchContext())
            {
                IQueryable<MyResult> searchItems = context.GetQueryable<MyResult>();
                Results = searchItems.Where(item => item.Title.Contains("V2")).ToList();
            }
        }
    }

Finally I render them to screen:

    <ul>
        <% foreach (var result in Results)
           { %>

        <li><%=result.Title %></li>

        <% } %>
    </ul>

Now we need to update the code above to support the Page Editor, since I have called my properties "ID" and "Title" then I can just rely on Glass' auto-mapping abilities, therefore the only change I need to make is to the HTML in my search page is to call the Editable method:

    <ul>
        <% foreach (var result in Results)
           { %>

        <li><%=Editable(result, x=>x.Title) %></li>

        <% } %>
    </ul>

Of course your property names may not always match the file names, in that case you can still use the Glass.Mapper attribute and fluent configuration:

        public class MyResult 
        {
            [TypeConverter(typeof(IndexFieldIDValueConverter))]
            [IndexField("_group")]
            public ID Id { get; set; }
            
            [IndexField("title")]
            public string Title { get; set; }
    
            [IndexField("computedvalue")]
            [SitecoreField("Text")]
            public string Computed {get;set;}
        }

Hopefully in this quick blog you have seen how easy it is to get the Page Editor working with any model using Glass.Mapper, even if that class hasn't been mapped by Glass.Mapper.

Glass needs your support!

Glass.Mappper.Sc is supported by the generous donations of the Glass.Mapper.Sc community! Their donations help fund the time, effort and hosting for the project.

These supporters are AMAZING and a huge thank you to all of you. You can find our list of supporters on the Rockstars page.

If you use Glass.Mapper.Sc and find it useful please consider supporting the project. Not only will you help the project but you could also get a discount on the Glass.Mapper.Sc training and join the Rockstars page.

Become a Rockstar