2

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.

model:

namespace demo.MVC.Models
{
    public class BlogItems 
    {  
      [SitecoreField("Title")]
      public virtual  string Title { get; set; }
      [SitecoreField("ThumbImage")]
      public virtual string ThumbImage { get; set; }
      [SitecoreField("EntryDate")]
      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;}
   }
}

Controller:

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

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> – Vlad Iobagiu Jan 23 '17 at 8:23
  • I already tried but it shows same issue – Indrasena Jan 23 '17 at 8:27
1

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

public interface IBaseItem

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

[SitecoreChildren]
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; }

[SitecoreInfo(SitecoreInfoType.TemplateId)]
Guid TemplateId { get; set; }

[SitecoreInfo(SitecoreInfoType.BaseTemplateIds)]
IEnumerable<Guid> BaseTemplateIds { get; set; }

[SitecoreInfo(SitecoreInfoType.TemplateName)]
string TemplateName { get; set; }

[SitecoreInfo(SitecoreInfoType.Name)]
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

0

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, privacy policy and cookie policy

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