Search code examples
sitecoresitecore6glass-mappersitecore-mvc

how to map sitecore items using glassmapper class in web froms..?


i'm creating demo project there i create Item which contains sub-Item now i want to render these using web controller my code like this site items created as following image enter image description here

and my glass mapper code is as:

  public static class GlassMapperSc
{
    public static void Start()
    {
        //create the resolver
        var resolver = DependencyResolver.CreateStandardResolver();

        //install the custom services
        GlassMapperScCustom.CastleConfig(resolver.Container);

        //create a context
        var context = Glass.Mapper.Context.Create(resolver);
        context.Load(
            GlassMapperScCustom.GlassLoaders()
            );

        GlassMapperScCustom.PostLoad();
    }

    public class DesktopHome
    {
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual string LeftRotatorTitle { get; set; }
        public virtual string RightRotatorTitle { get; set; }

    }
    public class GlobalsItem
    {
        public class HeaderTemplateItem
        {
            public class NavItem
            {
                public virtual string Title { get; set; }

                public virtual string Link { get; set; }

                public virtual IEnumerable<NavItem> Children { get; set; }

            }
        }
    }

}

i'm able to get parent items but not able to get child items please anyone help me to figure out this issue


Solution

  • Define your Modal Class as:

     [SitecoreClass]
     public class Header
     {
    
      [SitecoreInfo(SitecoreInfoType.Url)]
      public virtual string About{ get; set; }
    
      [SitecoreField]
      public virtual string Home{ get; set; }
    
      [SitecoreField]
      public virtual string Services{ get; set; }
    
      [SitecoreField]
      public virtual IEnumerable<Header> Links { get; set; }
    

    }

    Configuring the application

    To configure Glass Mapper is really straight forward. Open or create a Global.ascx file in your project and on the application start add the following code:

      protected void Application_Start(object sender, EventArgs e)
      {
          AttributeConfigurationLoader loader = new AttributeConfigurationLoader(
             new string[] { "Glass.Sitecore.Mapper.Demo.Application.Domain, Glass.Sitecore.Mapper .Demo" }
             );
          Persistence.Context  context = new Context(loader, null);
      }
    

    your view code will be as:

     <div>
      <h1>
          <asp:Literal runat="server" ID="About" />
      </h1>
      <div class="body">
          <asp:Literal runat="server" ID="Home" />
      </div>
      <div class="links">
          <asp:Repeater runat="server" ID="links">
              <HeaderTemplate>
                  <ul>
              </HeaderTemplate>
              <ItemTemplate>
                  <li><a href='<%# DataBinder.Eval(Container.DataItem,"Url") %>'>
                      <%# DataBinder.Eval(Container.DataItem,"Services") %></a> </li>
              </ItemTemplate>
              <FooterTemplate>
                  </ul>
              </FooterTemplate>
          </asp:Repeater>
      </div>
    

    Next lets look at the code behind page, for simplicity everything is going in the Page_Load method:

      protected void Page_Load(object sender, EventArgs e)
      {
          ISitecoreContext context = new SitecoreContext();
    
          DemoClass item = context.GetCurrentItem<DemoClass>();
          title.Text = item.Title;
          body.Text = item.Body;
    
          links.DataSource = item.Links;
          links.DataBind();
      }