1

Hi i am using glassmapper and i am trying to read all items in a multilist and populate.
My page has a a Navigation, Title and a multi list field where i can select the items . My problem is, while i am able to read the sub items (count is > 0) The property values are null. But Sitecore's basic Item properties are not null Fieldtype did not solve the problem These are my two Models

     public class Pagebase: ItemBase, INavigation
    {
        //Page Base

        public string PageTitle { get; set; }
        public string PageHeading { get; set; }

    //Navigation
        public string NavigationTitle { get; set; }
        public string NavigationDescription { get; set; }
    public IEnumerable<Pagebase> SubItems{ get; set; }
    }

[SitecoreType(TemplateId = "{7BC902B5-305B-484A-9AD9-6AAEBA48BDD7}", AutoMap = true)]
public interface INavigation 
{

    [SitecoreField("Navigation Title")]
        string NavigationTitle { get; set; }

        [SitecoreField("Navigation Description")]
        stringNavigationDescription { get; set; }

    [SitecoreField("Sub Items")]
        IEnumerable<Pagebase> SubItems{ get; set; }

}

My view is something like this

    @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<xxx.Pagebase>

    <div class-"test"> 
@Model.NavigationTitle // This has value 
@Model.NavigationDescription // This has correct value
@Model.SubItems.Count // Show the correct number of Items selected in Multi list. 

// The multilist is again a Pagebase type. 
//When i do : 
    @foreach (var subItem in Model.SubItems)
                            {
    @subItem.NavigationTitle            //This is null
    @subItem.NavigationDescription  // This is null 
    @@subitem.Id / @subitem.Url / @subitem.Name /  // This is not null 
    }
    </div>   

What am i missing??

Night Monger
  • 770
  • 1
  • 10
  • 33
  • I think you need the SitecoreType attribute on your Pagebase class. – DougCouto Dec 13 '16 at 21:13
  • Also, make sure you've published all of the templates and fields you created. – DougCouto Dec 13 '16 at 21:16
  • @Barbosa if that is the case - i shouldnt be getting the values for even the base item. I am getting all the values for the base item perfectly fine. Model.NavigationTile works just. it is only for multilist that fails – Night Monger Dec 13 '16 at 21:22
  • Anyone has any idea on this? – Night Monger Dec 13 '16 at 23:04
  • 1
    Try setting your properties to `virtual`, e.g. `public virtual string NavigationTitle { get; set; }` – jammykam Dec 14 '16 at 00:07
  • could you check base item children are published? also try adding Sitecore Field attribute on Pagebase class. – Dheeraj Palagiri Dec 14 '16 at 09:40
  • I tried both suggested by@jammykam and Dheeraj. Please note here that the Model is published. Since the Model (Pagebase) is recursively calling itself, the issue happens only for sub items. The Model itself has all values loaded properly. Model.NavigationTitle is Not nul.. Updated the question also. – Night Monger Dec 14 '16 at 15:45
  • Possible duplicate of [Why isn't my Enumerable getting populated by Glass.Mapper?](http://stackoverflow.com/questions/33131161/why-isnt-my-enumerable-getting-populated-by-glass-mapper) – Liam Dec 15 '16 at 11:49

1 Answers1

1

In the implementation class of the interface you will need to mark all properties as virtual.

See also the documentation of glassmapper

And the rationale of using virtual properties with glassmapper.

So, your implementation class will look like this

public class Pagebase: ItemBase, INavigation
{
    //Page Base
    public virtual string PageTitle { get; set; }
    public virtual string PageHeading { get; set; }

   //Navigation
    public virtual string NavigationTitle { get; set; }
    public virtual string NavigationDescription { get; set; }
    public virtual IEnumerable<Pagebase> SubItems{ get; set; }
}
Community
  • 1
  • 1
Nico Lubbers
  • 647
  • 1
  • 5
  • 10
  • at Everyone Thank you. I figured out the reason. virtual was one reason. But it did not work even after @jammykam suggested that. It was because of the stupid mistake i did. i had used droplist instead of droplink. I did not realise this until i checked back the template. I am marking jammykam as answer since he had replied first. – Night Monger Dec 16 '16 at 02:17