0

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

4

1 回答 1

2

将您的模态类定义为:

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

}

配置应用程序

配置 Glass Mapper 非常简单。在您的项目中打开或创建一个 Global.ascx 文件,然后在应用程序启动时添加以下代码:

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

您的视图代码将如下:

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

接下来让我们看看页面背后的代码,为简单起见,一切都在 Page_Load 方法中进行:

  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();
  }
于 2014-11-07T15:26:52.017 回答