如何在 Orchard 的单个页面上显示两个形状结果?
How to display two shape result on a single page in Orchard?
使用下面的代码,我有两个形状结果:
public ActionResult CompareRevisions(List<String> Ids)
{
contentItemLeft = // code to get a ContentItem
contentItemRight = // code to get a ContentItem
dynamic modelLeft = Services.ContentManager.BuildDisplay(contentItemLeft);
dynamic modelRight = Services.ContentManager.BuildDisplay(contentItemRight);
var ctx = Services.WorkContext;
ctx.Layout.Metadata.Alternates.Add("Layout_Null");
var shapeResultLeft = new ShapeResult(this, modelLeft);
var shapeResultRight = new ShapeResult(this, modelRight);
return shapeResultLeft;
}
当我在Controller的最后一行return任何一个形状结果如return shapeResultLeft
时,浏览器会完美显示内容。但是,如何同时在页面上显示我的两个 ShapeResults:shapeResultLeft
、shapeResultRight
?
如何 return ShapeResults 列表并使用 View/Layout 文件显示它?
您有多种选择:
方法一
MVC(非 Orchard 特定)中最常用的一个是视图模型:
public class MyViewModel {
public dynamic Shape1 { get; set; }
public dynamic Shape2 { get; set; }
}
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var viewModel = new MyViewModel {
Shape1 = modelLeft,
Shape2 = modelRight
}
return View(viewModel)
}
查看:
@model My.NameSpace.ViewModels.MyViewModel
@Display(Model.Shape1)
@Display(Model.Shape2)
方法二
在不使用强类型视图模型的情况下,您可以使用 orchard 的动态视图模型:
// inject IShapeFactory through Dependency Injection
public MyController(IShapeFactory shapeFactory) {
Shape = shapeFactory;
}
public dynamic Shape { get; set; } // inject with DI through IShapeFactory
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var viewModel = Shape
.ViewModel() // dynamic
.Shape1(modelLeft)
.Shape2(modelRight);
return View(viewModel);
}
方法三
或者对于 Orchard 的列表,当形状的数量可能不同时:
public dynamic Shape { get; set; } // inject with DI through IShapeFactory
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var list = Shape.List();
list.AddRange(myShapes); // myShapes is a collection of build shapes (modelLeft, modelRight)
var viewModel = Shape
.ViewModel()
.List(list);
return View(viewModel);
}
查看:
@Display(Model.List);
使用下面的代码,我有两个形状结果:
public ActionResult CompareRevisions(List<String> Ids)
{
contentItemLeft = // code to get a ContentItem
contentItemRight = // code to get a ContentItem
dynamic modelLeft = Services.ContentManager.BuildDisplay(contentItemLeft);
dynamic modelRight = Services.ContentManager.BuildDisplay(contentItemRight);
var ctx = Services.WorkContext;
ctx.Layout.Metadata.Alternates.Add("Layout_Null");
var shapeResultLeft = new ShapeResult(this, modelLeft);
var shapeResultRight = new ShapeResult(this, modelRight);
return shapeResultLeft;
}
当我在Controller的最后一行return任何一个形状结果如return shapeResultLeft
时,浏览器会完美显示内容。但是,如何同时在页面上显示我的两个 ShapeResults:shapeResultLeft
、shapeResultRight
?
如何 return ShapeResults 列表并使用 View/Layout 文件显示它?
您有多种选择:
方法一
MVC(非 Orchard 特定)中最常用的一个是视图模型:
public class MyViewModel {
public dynamic Shape1 { get; set; }
public dynamic Shape2 { get; set; }
}
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var viewModel = new MyViewModel {
Shape1 = modelLeft,
Shape2 = modelRight
}
return View(viewModel)
}
查看:
@model My.NameSpace.ViewModels.MyViewModel
@Display(Model.Shape1)
@Display(Model.Shape2)
方法二
在不使用强类型视图模型的情况下,您可以使用 orchard 的动态视图模型:
// inject IShapeFactory through Dependency Injection
public MyController(IShapeFactory shapeFactory) {
Shape = shapeFactory;
}
public dynamic Shape { get; set; } // inject with DI through IShapeFactory
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var viewModel = Shape
.ViewModel() // dynamic
.Shape1(modelLeft)
.Shape2(modelRight);
return View(viewModel);
}
方法三
或者对于 Orchard 的列表,当形状的数量可能不同时:
public dynamic Shape { get; set; } // inject with DI through IShapeFactory
public ActionResult CompareRevisions(List<String> Ids) {
// ..
var list = Shape.List();
list.AddRange(myShapes); // myShapes is a collection of build shapes (modelLeft, modelRight)
var viewModel = Shape
.ViewModel()
.List(list);
return View(viewModel);
}
查看:
@Display(Model.List);