0

在 Orchard CMS 中,我有一项服务可以从外部数据源中提取数据,并将数据加载到 Orchard Content Part 中。该部分有一个迁移,将它与标题部分焊接在一起,我有一条路线,以便通过 URL 访问我的控制器:

我正在使用控制器通过 URL 访问项目,就像博客部分控制器一样。但是我无法呈现我的部分......博客控制器类似于以下内容:

    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    var model = _services.ContentManager.BuildDisplay(asset); 

    return new ShapeResult(this, model);

但是如果我这样做,“BuildDisplay”方法会查找asset.ContentItem,但这是空的,尽管我的部分来自“ContentPart”。

我需要做什么才能显示我的数据?

4

2 回答 2

0

通过让我的部分派生自 ContentPart,我可以使用以下 Controller 方法:

private readonly IAssetService _assetService;
private readonly IOrchardServices _services;

public MyController(IShapeFactory shapeFactory, IAssetService assetService, IOrchardServices services) {
    _assetService = assetService;
    _services = services;
    Shape = shapeFactory;
}

public dynamic Shape { get; set; }

public ActionResult MyAction(int assetId) {
    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    // this method assumes you have a view called Parts.Asset.cshtml (see the AssetPartDriver)
    var model = _services.ContentManager.New("Asset");
    var item = contentItem.As<AssetPart>();
    item.Populate(asset) // Method that just populates the service loaded object into the ContentPart

    return new ShapeResult(this, _services.ContentManager.BuildDisplay(item));
}

这将使用“AssetPartDriver”:

public class AssetPartDriver : ContentPartDriver<AssetPart>
    {
        protected override DriverResult Display(AssetPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Asset", () => shapeHelper.Parts_Asset()); // Uses Parts.Asset.cshtml
        }
    }

并结合屏幕上呈现的“Placement.info”文件:

<Placement>
  <Match ContentType="Asset">
    <Match DisplayType="Detail">
      <Place Parts_Asset="Content"/>
    </Match>
  </Match>
</Placement>

迁移文件将我的 Web 服务部分与其他 Orchard 部分结合起来:

public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            ContentDefinitionManager.AlterTypeDefinition("Asset", cfg => cfg
                .WithPart("AssetPart")
                .WithPart("AutoroutePart", builder => builder
                    .WithSetting("AutorouteSettings.AllowCustomPattern", "True"))
                .Listable()
                .Securable()
                .Creatable(false));

            ContentDefinitionManager.AlterPartDefinition("AssetPart", part => part
                .WithDescription("A part that contains details of an individual Web Service loaded asset."));
            return 1;
        }
    }

这些附加部件尚未使用,但可以在创建期间填充并使用放置文件单独显示。这是我努力实现的第一步!!

于 2015-09-21T08:08:28.180 回答
0

如果我理解正确,您正在尝试仅显示一部分,而不是整个内容项。

要显示单个形状,您可以执行以下操作:

private readonly IAssetService _assetService;

public MyController(IShapeFactory shapeFactory, IAssetService assetService) {
    _assetService = assetService;
    Shape = shapeFactory;
}

public dynamic Shape { get; set; }

public ActionResult MyAction(int assetId) {
    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    // the shape factory can call any shape (.cshtml) that is defined
    // this method assumes you have a view called SomeShapeName.cshtml
    var model = Shape.SomeShapeName(asset);

    return new ShapeResult(this, model);
}

!!注意:这不会踢出零件的(显示)驱动程序,它只会返回给定模型的 .cshtml

于 2015-09-11T14:28:01.033 回答