Sitecore Glass Mapper ObjectToSwitchTo null reference in page editor

*爱你&永不变心* 提交于 2019-12-14 03:13:16

问题


I have the following structure as Template in visual studio :

Under a page, i have one or more link root

[SitecoreType(TemplateId = "{4AAA9A10-36C2-484F-A648-2BEF349F0052}", AutoMap = true)]
public class LinkRoot : IBaseTemplate
{
    [SitecoreChildren(InferType = true)]
    public virtual IEnumerable<LinkItem> Children { get; set; }

    [SitecoreInfo(SitecoreInfoType.TemplateId)]
    public virtual Guid TemplateId { get; set; }
    public Guid Id { get; set; }
    public string Language { get; set; }
    public ItemUri Uri { get; set; }
    public int Version { get; private set; }


}

Under the link root i've LinkItems

[SitecoreType(AutoMap = true)]
public class LinkItem : IBaseTemplate
{
    [SitecoreField("Link Name")]
    public virtual string LinkName { get; set; }

    [SitecoreField("Link")]
    public virtual Link Link { get; set; }

    public Guid Id { get; set; }
    public string Language { get; set; }
    public ItemUri Uri { get; set; }
    public int Version { get; private set; }
}

I display those items in a view like that :

 @foreach (var link in Model.Children.Where(o => o.TemplateId.Equals(TemplateIDs.LinksRoot.Guid)))
    {
        foreach (var linkChildren in link.Children)
        {
            using (BeginRenderLink(linkChildren, x => x.Link, isEditable: true))
            {
        @Editable(linkChildren, x => x.LinkName)
            }
        }
    }

It works great, i can see my links with the good name etc, but when i go to the page editor i got this error :

Value cannot be null. Parameter name: objectToSwitchTo

at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName) at Sitecore.Common.Switcher2.Enter(TValue objectToSwitchTo) at Sitecore.Data.Items.ContextItemSwitcher..ctor(Item item) at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression1 field, Expression`1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer)

Does someone already experienced that or have an idea why i have this error ?

Thanks


回答1:


I think it means that Glass can't resolve the template of the LinkItem model.
Instead of:

[SitecoreType(AutoMap = true)]
public class LinkItem : IBaseTemplate

Try to explicitly define the template ID:

[SitecoreType(TemplateId = "{your-template-guid}", AutoMap = true)]
public class LinkItem : IBaseTemplate



回答2:


I think this might be a bug, can you log it on github as an issue.



来源:https://stackoverflow.com/questions/22129642/sitecore-glass-mapper-objecttoswitchto-null-reference-in-page-editor

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