I am trying to get the items from sitecore based on template id using controller rendering.i am using glass mapper also.but values are not passed in to model.finally i am getting null reference exception. here is my code.


namespace demo.MVC.Models
    public class BlogItems 
      public virtual  string Title { get; set; }
      public virtual string ThumbImage { get; set; }
      public virtual  string EntryDate { get; set; }  

   [SitecoreQuery("/sitecore/Content/demomvc/Home//*[@@templateid='{C016F8F2-500C-41E0-8FC7-CB2450702EDB}']", IsRelative = false)]
   public class blogs
     public virtual IEnumerable<BlogItems> blogpage{get;set;}


public class BindBlogsController : GlassController
    // GET: BindBlogs
    public ActionResult bind()
       return view();


@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView <demo.MVC.Models.blog>

@foreach(var item in Model.blogPage)
    @Html.Sitecore().Field("Title", item)    

finally it shows the error

"System.NullReferenceException: Object reference not set to an instance of an object."

I am using glass mapper 4.2 version,

can anyone suggest how to retrieve the items from sitecore? Thanks in advance

  • can you replace your 3rd line from view with @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<demo.MVC.Models.BlogItems> Jan 23, 2017 at 8:23
  • I already tried but it shows same issue
    – Indrasena
    Jan 23, 2017 at 8:27

2 Answers 2


An easy way for you to do that is have a base class like this:

public interface IBaseItem

{ [SitecoreId] Guid Id { get; set; }

IEnumerable<Item> Children { get; set; }

[SitecoreInfo(SitecoreInfoType.Url, UrlOptions = SitecoreInfoUrlOptions.LanguageEmbeddingNever)]
string Url { get; set; }

[SitecoreInfo(SitecoreInfoType.Language, UrlOptions = SitecoreInfoUrlOptions.LanguageEmbeddingNever)]
Language Language { get; set; }

[SitecoreInfo(SitecoreInfoType.Version, UrlOptions = SitecoreInfoUrlOptions.LanguageEmbeddingNever)]
int Version { get; set; }

Guid TemplateId { get; set; }

IEnumerable<Guid> BaseTemplateIds { get; set; }

string TemplateName { get; set; }

string Name { get; set; }


then you can make your class inherit from it and reuse on other classes you want to map. another thing I'm missing on your class is the attribute on your class like this:

[SitecoreType(TemplateId = "{A504745B-43E8-4594-B9B3-FACBB10FB29B}")]
public class BlogItems : IBaseItem
{ ..... }

Also as a suggestions: on your view you can use the Glass helper methods. Instead of this:

@Html.Sitecore().Field("Title", item)

you can have it strongly typed with your model:

 @Html.Glass().Editable(Model, x => x.Title)

hope this helps


Try the following:

public ActionResult bind()
    ISitecoreContext service = new SitecoreContext();
    var model = service.GetCurrentItem<blogs>();
    return View(model);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.