2015-09-17 46 views
1

使用下面的代碼,我有兩個形狀的結果:如何在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; 
} 

當我在控制器的最後一行返回任何一種形狀的結果,例如return shapeResultLeft的,瀏覽器完美顯示該內容。但是,如何在同一頁面上同時顯示我的兩個ShapeResults:shapeResultLeft,shapeResultRight

如何退還ShapeResults的列表,並使用查看/版式文件顯示呢?

回答

5

您有這多種選擇:

方法1在MVC(未果園專用)最常用的

一個是視圖模型:

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

不使用強類型的ViewModels,您可以使用果園的動態視圖模型:

// 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

或者與果園的列表中,當形狀的數量可能會有所不同:

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); 
+0

能否請你告訴我什麼是「通過IShapeFactory與DI注入」是什麼意思?什麼是DI? 。 Iam只是一個新手,我很抱歉問這個小細節 –

+0

見方法2編輯答案 – devqon

+0

感謝百萬噸!完美地完成了上述3種方法。 –

相關問題