【问题标题】:Sitecore Glass mapper GetItem<TypeName>(itemPath) returns null when casting as custom typeSitecore Glass 映射器 GetItem<TypeName>(itemPath) 在转换为自定义类型时返回 null
【发布时间】:2015-10-21 04:26:26
【问题描述】:

当我使用 Glass Mapper 的 SitecoreContext().GetItem&lt;Sitecore.Data.Items.Item&gt;(ItemPath) 时,它可以正常工作,但一旦我将它与由 TDS + Glass 生成的自定义模型一起使用,它就会返回 null

我的代码如下:

NavigationHelper.cs

public class NavigationHelper
{
    public static int GetChildCount()
    {
        var context = new SitecoreContext();
        string g = "/sitecore/content/home/Site Settings/Top Navigation Items";

        var t = context.GetItem<Models.sitecore.templates.Navigation.Navigation_Item_Folder>(g);
        var v = context.GetItem<Items.Item>(g);
        // t is always null, but v always finds the item correctly.
        return t.Children.Count();
    }
}

Models.sitecore.templates.Navigation.Navigation_Item_Folder是Glass和TDS生成的模型,如下:

/// <summary>
/// Navigation_Item_Folder
/// <para></para>
/// <para>Path: /sitecore/templates/Navigation/Navigation Item Folder</para>    
/// <para>ID: c0f00c6b-145e-47f2-9427-57df58a612f1</para>   
/// </summary>
[SitecoreType(TemplateId=INavigation_Item_FolderConstants.TemplateIdString)] //, Cachable = true
public partial class Navigation_Item_Folder  : GlassBase, INavigation_Item_Folder 
{

}

它只是一个文件夹,所以它上面没有字段。我已经尝试扩展这个部分类以包含一个标题字段,但这并没有解决问题。

我已经尝试过这里的步骤:Sitecore Glass mapper GetItem<TypeName>(guid) always return null 但他们没有帮助。

【问题讨论】:

  • Sitecore API 将返回一个 Item 对象(如果存在),但 Glass 只会在当前语言也有任何版本时才返回对象。您可以检查 v.Versions 是否有。我相信 v4 有一个配置设置来修改行为。

标签: sitecore glass-mapper sitecore-mvc tds


【解决方案1】:

你能试试{C0F00C6B-145E-47F2-9427-57DF58A612F1}这个guid吗?我已经看到标准 Sitecore 查询在通过模板 ID 定位项目时存在区分大小写的问题。

【讨论】:

  • 我相信这就是问题所在。我们在.tt 文件中为xxxxxConstants 类部分中的TemplateIdString 字段添加了.ToUpper()。如果您使用的是glassv3item.tt,则在第 99 行附近。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多