2011-12-10 38 views
2

場景:有三個視圖(Razor)名爲「InvoiceBill」,「Index」,「Create」。在MVC3和Razor中從單個動作返回多個視圖

  1. 創建視圖包含用於輸入發票數據的表單頁面。
  2. 索引頁面包含所有當前日期發票賬單的列表。
  3. InvoiceBill包含基於此鏈接生成Excel文檔(作爲發票報告)的代碼(http://forums.asp.net/t/1711971.aspx/1)。

一旦我提交表單(創建視圖),它可以將表單數據發佈到數據庫(使用EF)。插入數據後,它返回索引頁面。

但現在我需要在調用索引頁之前調用「InvoiceBill」視圖。

[HttpPost] 
public ActionResult Create(FormCollection collection, InvoiceViewModel INVViewModel) 
{ 
    if ((collection != null) && (this.ModelState.IsValid)) 
    { 
     salesOrder.AccountNumber = INVViewModel.AccountNumber; 
     salesOrder.BillToAddressID = INVViewModel.BillToAddressID; 
     salesOrder.Comment = INVViewModel.Comment; 
     salesOrder.ContactID = INVViewModel.ContactID; 
     . 
     . 
     . 
     . 
     int sID = Using<CreateSalesOrder>().Execute(0, salesOrder); 
***** From here i need to call the InvoiceBill page. Or guide me any other good way to achieve this 
     return RedirectToAction("Index"); 
    } 
} 

現在我正在通過一些解決方法實現此目標。但我覺得這不好。請指導我如何以正確的方式實現這一目標? 感謝

編輯:

public ActionResult PrintInvoice(int id) 
     { 
      InvoiceReportViewModel invoiceReport = new InvoiceReportViewModel(); 
      var soToView = Using<GetSalesOrders>().ExecuteForReport(id); 

      invoiceReport.InvoiceBillName = "Invoice" + id.ToString(); 
      invoiceReport.CustomerName = soToView.Customer.Name; 
      invoiceReport.SalesOrderNumber = soToView.SalesOrderNumber; 
      . 
      . 
      . 
      . 

      return View(invoiceReport); 
     } 

的InvoiceReport.cshtml

@model eItemBox.Web.Models.InvoiceReportViewModel 
@{ 
    Layout = null; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Model.InvoiceBillName); 
    //Content-Disposition is defined in RFC-2183 
} 
<Worksheet ss:Name="MyInvoice"> 
. 
. 
. 

</Workbook> 
+0

你想返回多個視圖或不同的視圖? – saintedlama

+0

不同意見 – getmk

回答

0

我想你的問題是非常相似的this question。唯一的區別是,您需要將輸出保存爲Excel,而不是將其作爲電子郵件發送。

請嘗試從上面的問題使用RenderViewToString方法:

public virtual string RenderViewToString(
    ControllerContext controllerContext, 
    string viewPath, 
    string masterPath, 
    ViewDataDictionary viewData, 
    TempDataDictionary tempData) 
{ 
    Stream filter = null; 
    ViewPage viewPage = new ViewPage(); 

    //Right, create our view 
    viewPage.ViewContext = new ViewContext(controllerContext, new WebFormView(viewPath, masterPath), viewData, tempData); 

    //Get the response context, flush it and get the response filter. 
    var response = viewPage.ViewContext.HttpContext.Response; 
    response.Flush(); 
    var oldFilter = response.Filter; 

    try 
    { 
     //Put a new filter into the response 
     filter = new MemoryStream(); 
     response.Filter = filter; 

     //Now render the view into the memorystream and flush the response 
     viewPage.ViewContext.View.Render(viewPage.ViewContext, viewPage.ViewContext.HttpContext.Response.Output); 
     response.Flush(); 

     //Now read the rendered view. 
     filter.Position = 0; 
     var reader = new StreamReader(filter, response.ContentEncoding); 
     return reader.ReadToEnd(); 
    } 
    finally 
    { 
     //Clean up. 
     if (filter != null) 
     { 
     filter.Dispose(); 
     } 

     //Now replace the response filter 
     response.Filter = oldFilter; 
    } 
} 

UPDATE: 這樣做的另一個(有點不同)的方式,可以發現here

+0

謝謝亞歷克斯。但不太可能我在Excel文檔中遇到以下錯誤。 「發送HTTP標頭後無法重定向。」 – getmk

+0

我想從更新的鏈接是關於處理完全相同的情況:http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html –

+0

查看單詞,'HttpContext。當前'應該使用,而不是'controllerContext.HttpContext.Response.Filter' –

相關問題