如何在Web表单中使用glass mapper类映射sitecore项目..?

时间:2014-11-07 14:36:20

标签: sitecore sitecore6 glass-mapper sitecore-mvc

我正在那里创建演示项目我创建包含子项目的项目现在我想使用web控制器呈现这些我的代码像这样 网站项目创建如下图像     enter image description here

我的玻璃映射器代码如下:

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

            }
        }
    }

}

我能够获得父项但无法获得子项目请任何人帮我解决此问题

1 个答案:

答案 0 :(得分: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();
  }