如何使用Glass Mapper实现Sitecore模板继承 [英] How to implement Sitecore Template inheritance with Glass Mapper

查看:78
本文介绍了如何使用Glass Mapper实现Sitecore模板继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过Glass Mapper实现以下目标,但无法使其正常工作.

I've been trying to achieve the following with glass mapper but can't get it to work.

我有一个主页模板,该模板本身没有任何字段,但是继承了以下两个模板:

I have a Home Page template which doesn't have any fields itself but inherits the following two templates:

Navigation Template

Fields: Navigation Title

Meta Information Template

Fields: Page Title, Meta Description

我已经创建了对应的接口/类,如下所示:

I've created the corresponding interfaces / classes as follows:

[SitecoreType(TemplateId = "{5BAB563C-12AD-4398-8C4A-BF623F7DBCDC}", AutoMap = true)]
public interface INavigation
{
    [SitecoreField(FieldName = "Navigation Title")]
    string NavigationTitle { get; set; }
}

[SitecoreType(TemplateId = "{95539498-31A5-4CB5-8DD6-C422D505C482}", AutoMap = true)]
public interface IMetaInformation
{
    [SitecoreField]
    string PageTitle { get; set; }

    [SitecoreField]
    string MetaDescription { get; set; }
}

[SitecoreType(TemplateId = "{F08693E5-8660-4B13-BBD6-7B9DC6091750}", AutoMap = true)]
public class HomePage : INavigation, IMetaInformation
{
    public virtual string NavigationTitle { get; set; }

    public virtual string PageTitle { get; set; }

    public virtual string MetaDescription { get; set; }
}

当我尝试访问我的页面时,所有属性始终为空:

When I then try accessing my page all attributes are always null:

var context = new SitecoreContext();
var page = context.GetCurrentItem<HomePage>();

我已经尝试了几种不同的方法来解决这个问题,但是没有任何效果.同样,不同教程中描述的内容也不起作用.唯一有效的方法是直接在主页"模板上添加字段,但是我不希望这样做,因为我有多个页面类型,因此我想继承这些字段.

I've tried several different approaches to this but nothing works. Also what was described in different tutorials didn't work. The only thing that works is when I add the fields directly on the Home Page template, but I don't want that since I have more than one page type and I therefore want to inherit the fields.

有人知道我在这里缺少什么吗?!顺便说一下,我正在将Sitecore 7与.NET 4.5结合使用.

Does anyone have any idea what I'm missing here?! I'm using Sitecore 7 with .NET 4.5 by the way if that makes a difference.

推荐答案

您的字段未映射,因为您在Sitecore模板的字段名称"中使用了空格. 要么删除空间,要么将属性[SitecoreField(FieldName ="Page Title")]添加到模型中.

Your fields are not mapped because you use a space in the Fieldname in the Sitecore Template. Either remove the space or add the attribute [SitecoreField(FieldName ="Page Title")] to the Model.

我认为Homepage类正在尝试使用fieldName NavigationTitle映射Homepage模板上的NavigationTitle,而忽略基本模型上的FieldName属性.

I think that the Homepage class is trying to map the NavigationTitle on the Homepage template with the fieldName NavigationTitle and ignores the FieldName attribute on the base model.

顺便说一句:我正在为我正在处理的当前项目仅使用接口,并且它在继承时可以正常工作.无需再添加一次属性;)

By the way: I am using only interfaces for the current project I'm working on and it works as expected with inheritance. No need to add a property more then once ;)

这篇关于如何使用Glass Mapper实现Sitecore模板继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆