场景:存在名为“InvoiceBill”、“Index”、“Create”的三个 View (Razor)。
- Create View 包含用于输入发票数据的表单页面。
- 索引页包含所有当前日期发票账单的列表。
- InvoiceBill 包含基于此链接 (http://forums.asp.net/t/1711971.aspx/1) 生成 excel 文档(作为发票报告)的代码。
一旦我提交表单(创建 View ),它就可以将表单数据发布到数据库(使用 EF)。插入数据后,返回索引页。
但现在我需要在调用索引页面之前调用“InvoiceBill” View 。
[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>
最佳答案
我想您的问题与 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;
}
}
更新: 可以找到另一种(有点不同)的方法 here
关于c# - 从 MVC3 和 Razor 中的单个操作返回多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454892/