In my asp.net mvc 2 application, why are parameters passed via RedirectToAction sometimes lost?

464 views Asked by At

In my asp.net mvc 2 application, why are parameters passed via RedirectToAction sometimes lost?

This perplexes me. Sometimes using RedirectToAction works, sometimes it doesn't. I haven't found any rhyme or reason to it. Here's an example:

        return RedirectToAction("ExportReport",
            new { FieldOrder = fieldOrder });

I've also tried this variant:

        return RedirectToAction("ExportReport", "SearchAndExport",
            new { FieldOrder = fieldOrder });

I added the following in my global.asax.cs:

        routes.MapRoute(
            "ExportReport",
            "{controller}.aspx/{action}/{FieldOrder}",
            new { controller = "SearchAndExport", action = "ExportReport", FieldOrder = UrlParameter.Optional }
          );

And lastly, the prototype for the action I'm redirecting to:

public FileContentResult ExportReport(List<String> FieldOrder)

FieldOrder's data never makes it to the redirected action. Why? I know I've done this and had it work many times as well. What gives?

EDIT 1

To make it perfectly clear, FieldOrder is a List<String>. I am not using site areas. The redirect itself works, but the data is not passed to the method. I have verified that there is data that should be passed via debug.

Also, I'd rather not use TempData. If you have more than one action redirect to another more general action, that becomes messy. It's cleaner (IMHO) to just pass the data via the parameter.

2

There are 2 answers

0
jason On BEST ANSWER

Going over some of my old questions here...

The reason these redirect values were getting lost was that I was passing a complex object as a parameter. ASP.NET MVC 2 did not handle these. Redirects with primitive type parameters will work fine.

1
Matija Grcic On

Store your FieldOrder in TempData (holds the data for only one redirect) http://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary.aspx