在Orchard CMS中,我有一项服务从外部数据源提取数据,并将数据加载到Orchard内容部分。 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,但这是null,尽管从'ContentPart'中获取了我的部分。
我需要做什么才能显示数据?
答案 0 :(得分: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
答案 1 :(得分: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));
}
这将使用&#39; AssetPartDriver&#39;:
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&#39;一起使用。文件呈现在屏幕上:
<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;
}
}
这些附加部分尚未使用,但可以在创建过程中填充并使用展示位置文件单独显示。 这是我试图实现的第一步!!