获取Orchard中自定义部件的媒体选择器字段内容

时间:2015-01-04 18:17:28

标签: orchardcms

我使用内容选择器字段创建了自定义部件。

public int UpdateFrom1()
    {
        ContentDefinitionManager.AlterPartDefinition("BackgroundPart",
            builder => builder.WithField("BackgroundImage",
                fieldBuilder => fieldBuilder
                    .OfType("MediaPickerField")
                    .WithDisplayName("Background Image")));

        return 2;
    }

    public int UpdateFrom2()
    {
        ContentDefinitionManager.AlterTypeDefinition("Background", cfg => cfg             
          .WithPart("BackgroundPart")
          .Creatable()
          .Indexed());
        return 3;
    }

获取数据的服务代码:

public class BackgroundService : IBackgroundService
{       
    private readonly IRepository<BackgroundPartRecord> _repository;

    public BackgroundService(
        IRepository<BackgroundPartRecord> repository,
        ISignals signals)
    {
        _repository = repository;           
    }

    public IEnumerable<BackgroundPartRecord> Get()
    {
        return _repository.Table;
    }       
}

这有效(我可以在创建此类型的新项目时选择内容)。

现在我想获得我所有类型的项目列表。我为此创建了一个服务,并获得了我创建的项目列表。但列表中的项目没有媒体选择器字段。我如何获得这些内容?我想在我的模块中的FilterProvider类中的OnResultExecuting方法中使用它。

1 个答案:

答案 0 :(得分:1)

由于您正在使用存储库API,因此无效。存储库是内部使用的低级API,但如果模块使用它,则应该很少使用。其中一个原因是它不会获取内容项,只是部分记录。

相反,您需要使用ContentManager中的一个查询API。这将为您提供可以As开启的真实内容项,这将使您可以访问内容项的字段(存储在Infoset上,位于内容项记录上)等。

这个或其中一个重载和扩展方法应该可以解决这个问题:

_contentManager.Query<BackgroundPart>()
相关问题