传递信息使用RedirectToAction另一个动作 - MVC [英] Passing info to another action using RedirectToAction - MVC

查看:180
本文介绍了传递信息使用RedirectToAction另一个动作 - MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的动作:

    public ActionResult Report(AdminReportRequest reportRequest, FormCollection formVariables)
    {
        AdminEngine re = new AdminEngine();

        AdminReport report = re.GetCompleteAdminReport(reportRequest);

        return View(report);
    }



我不知道我怎么会去同一个控制器内重定向到另一个动作?,传递AdminReportRequest和的FormCollection变量

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(字符串,
字符串,
System.Web.Routing.RouteValueDictionary) '
有一些无效参数

'System.Web.Mvc.Controller.RedirectToAction(string, string, System.Web.Routing.RouteValueDictionary)' has some invalid arguments

参数3:无法从
'System.Web.Mvc.FormCollection'到
转换'System.Web.Routing.RouteValueDictionary

Argument 3: cannot convert from 'System.Web.Mvc.FormCollection' to 'System.Web.Routing.RouteValueDictionary'

如果有人能在正确的方向我会非常感激它指向我,我想我可能需要编辑然而在Global.asax文件我不是太熟悉。

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.

感谢。

推荐答案

您可以使用的 TempData的对象。

到TempData属性值存储在会话状态。该TempDataDictionary值设置可以从对象的值,然后处理或显示它们后调用任何操作方法。 TempData的价值仍然存在,直到它被读取或直到会话超时

The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out.

这个MSDN文章解释这一切。

public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
   //...
   TempData["Report"] = reportRequest;  //store to TempData
   //...
}

public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
  //...
  var report = TempData["Report"] as AdminReportRequest;
  //...
}

这篇关于传递信息使用RedirectToAction另一个动作 - MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆