1

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.

109221793
  • 16,477
  • 38
  • 108
  • 160
  • The method signature is 'RedirectToAction(String, String, RouteValueDictionary)'. But you try to pass the third parameter of type 'FormCollection' to this method. – Egor4eg Apr 22 '11 at 08:48

1 Answers1

4

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;
  //...
}
David Glenn
  • 24,412
  • 19
  • 74
  • 94
  • Hi David, TempData seems to be just what I need, thanks. Just one thing - I have implemented like above, however in my redirect action it doesn't seem to be picking the values up. I have debugged through and but var report, etc are null. Any ideas? I'll mark this as correct anyhow because TempData is what I need. – 109221793 Apr 22 '11 at 09:38
  • Scrap that - I got it ;-), Thanks – 109221793 Apr 22 '11 at 09:52