Sunday, 3 August 2014

Error realted to Item Id while working with Glass Mapper in Sitecore page editor mode

Leave a Comment
Today I was working on Sitecore Glass Mapping framework for my Sitecore MVC project. I’ve installed Glass Mapper from nuget and configured it for Sitecore MVC project. For more information on the Glass.Sitecore.Mapper visit the official website. Thanks Mike and Tom for this great framework. Below are the few details about my development environment:
  • MVC Version:  4Sitecore Version:  7.0
  • Glass.Mapper version:  3.0.10.23
  • Glass.Mapper.Sc version:  3.2.0.39
  • Glass.Mapper.Sc.Mvc version:  3.2.0.35
  • Glass.Mapper.Sc.Razor version:  3.0.9.13
I am using SimpleInjector IOC container to create objects that are used by Glass.Mapper rather than default Castle Windsor or default inbuilt Glass.Mapper method. Using other IOC container with Glass.Mapper is very easy and I got my webpage up and running in Sitecore normal mode easily. However I got below error while working in Sitecore Page Editor Mode.
You can not save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: SitecoreRamblings.Models.NewsModel
   at Glass.Mapper.Sc.Configuration.SitecoreTypeConfiguration.ResolveItem(Object target, Database database)
   at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression`1 field, Expression`1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer)
Below is the implementation of Model class:
using Glass.Mapper.Sc.Configuration.Attributes;
using SitecoreRamblings.Service.Interface;

namespace SitecoreRamblings.Models
{
    [SitecoreType(AutoMap = true)]
    public class NewsModel
    {
        private readonly INewsService _service;       
        public virtual string Title { get; set; }
        public virtual string Body { get; set; }
        public virtual string Abstract { get; set; }

        public NewsModel(INewsService service)
        {
            _service = service;
        }     
    }
}
After investigation; I’ve realized that I forgot [my stupidity on peak :( ] to add an additional property in NewsModel class to represent the Sitecore ID. I’ve added below property in NewsModel class:
[SitecoreId]
public virtual Guid Id { get; set; }
Sitecore ID is required to allow Glass.Mapper to link your model to the actual Sitecore item in Page Edit mMode. After building solution, I was no longer getting error in Page Editor Mode.
Comments and suggestions are most welcome. Happy coding! 

0 comments :

Post a Comment