Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

c# - Passing info to another action using RedirectToAction - MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the TempData object.

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.

This MSDN article explains it all.

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;
  //...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...