How to implement Sitecore Template inheritance with Glass Mapper

和自甴很熟 提交于 2019-12-11 01:44:20

问题


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.

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.


回答1:


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.

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




回答2:


Try to set infer type to true. I cannot get it to work at all without having that set.

ex.

item.GlassCast<HomePage>(false, true);

or

context.GetCurrentItem<HomePage>(false, true);

I find it does not work without this set.




回答3:


You should render the common fields in a separate sublayout as a GlassUserControl.

public partial class NavigationTemplate : GlassUserControl<NavigationTemplate>
{

    protected void Page_Load(object sender, EventArgs e)

Here you will have direct access to the NavigationTemplate fields no matter what item you are loading, it will always be cast to NavigationTemplate and will read the field values of the item you are loading.




回答4:


It seems that you expect to get the properties on the HomePage instance, but you need to ask for the exact interface that contains the property as seen here

I.e. Instead of doing:

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

You should explicitly get current item as INavigation and get the field from the interface:

var navigationTitle = context.GetCurrentItem<INavigation>().NavigationTitle;


来源:https://stackoverflow.com/questions/19568427/how-to-implement-sitecore-template-inheritance-with-glass-mapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!