场景:有三个视图(Razor)名为“InvoiceBill”,“Index”,“Create”。
一旦我提交表单(创建视图),它就可以将表单数据发布到DB(使用EF)。插入数据后,将返回“索引”页面。
但现在我需要在调用Index页面之前调用“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 :(得分: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;
}
}
<强>更新强> 可以找到另一种(有点不同)的方法here