razormodel-view-controllersitecoreglass-mappertds

Glassmapper Sitecore Multilist field is empty


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??


Solution

  • 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; }
    }