如何在Orchard的单个页面上显示两个形状结果?

时间:2015-09-17 23:10:14

标签: c#-4.0 orchardcms orchardcms-1.8

使用下面的代码,我有两个形状结果:

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 shapeResultLeft)时,浏览器会完美显示内容。但是,如何同时在页面上同时显示我的ShapeResults:shapeResultLeftshapeResultRight

如何返回ShapeResults列表并使用View / Layout文件显示它?

1 个答案:

答案 0 :(得分:5)

您有多种选择:

方法1

最常用于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)

方法2

不使用强类型视图模型,您可以使用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);
}

方法3

或者使用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);