使用 RedirectToAction 将信息传递给另一个操作 - MVC
我有这个操作:
public ActionResult Report(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
我想知道如何重定向到同一控制器内的另一个操作,传递 AdminReportRequest 和 FormCollection 变量?
我心里有这样的想法:
public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
if (!reportRequest.Download)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
return RedirectToAction("ExcelSalesReport", reportRequest, formVariables);
}
public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
Stream SalesReport = re.GetExcelAdminReport(reportRequest);
return new FileStreamResult(SalesReport, "application/ms-excel")
{
FileDownloadName = "SalesReport" + DateTime.Now.ToString("MMMM d, yyy") + ".xls"
};
}
这显然是错误的,并且会引发一些错误,例如:
'System.Web.Mvc.Controller.RedirectToAction(字符串, 细绳, System.Web.Routing.RouteValueDictionary)' 有一些无效参数
并且
参数 3:无法从 'System.Web.Mvc.FormCollection' 到 'System.Web.Routing.RouteValueDictionary'
如果有人能指出我正确的方向,我将不胜感激,我想我可能必须编辑 Global.asax 文件,但我对此不太熟悉。
谢谢。
I have this action:
public ActionResult Report(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
I was wondering how could I go about Redirecting to another action within the same controller, passing the AdminReportRequest, and FormCollection variables?
I had something like this in mind:
public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
if (!reportRequest.Download)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
return RedirectToAction("ExcelSalesReport", reportRequest, formVariables);
}
public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
Stream SalesReport = re.GetExcelAdminReport(reportRequest);
return new FileStreamResult(SalesReport, "application/ms-excel")
{
FileDownloadName = "SalesReport" + DateTime.Now.ToString("MMMM d, yyy") + ".xls"
};
}
This is obviously wrong and throws up some errors, such as:
'System.Web.Mvc.Controller.RedirectToAction(string,
string,
System.Web.Routing.RouteValueDictionary)'
has some invalid arguments
and
Argument 3: cannot convert from
'System.Web.Mvc.FormCollection' to
'System.Web.Routing.RouteValueDictionary'
If someone could point me in the right direction I'd greatly appreciate it, I think I might have to edit the Global.asax file however I'm not overly familiar with this.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 TempData 对象。
这篇 MSDN 文章 解释了这一切。
You can use the TempData object.
This MSDN article explains it all.