Search code examples
asp.net-mvcredirecttoactionroutevalues

In MVC, why does the routeValues property in RedirectToAction() not accept my class as argument?


So here's the deal, i want to be able to export any Enumerable of items to excel:

Here's an ActionMethod in some Area of my app that constructs an "ExportToExcel" model, then Redirects it to an Action Method in another controller and another are which does all the formatting-to-excel work:

public ActionResult ExportCustomListToExcel()
{
    var exportModel = new ExportToExcelModel();

    //Here I fill up the model with a dataTable / other file info like
    //exportModel.Items = blah blah..

    return RedirectToAction("ExportToExcel", "Shared", new { model = exportModel, testString = "test",  area = "Shared" });
}

And here's my Shared ExportToExcel ActionMethod:

public ActionResult ExportToExcel(ExportToExcelModel model, string testString)
{
    //PROBLEM IS RIGHT HERE!
    // where testString == "test"
    // but model == null :(


    //Ommited unrelated code
}

My ExportToExcel actionMethod gets hit, but somewhere along the way my ExportToExcelModel gets lost :(

Note: It succeeds on passing strings like "testString" so is there somwthing wrong with my model?

Just in case, the ExportToExcelModel is:

public class ExportToExcelModel
{
    public ExportToExcelModel() {}

    public ExportToExcelModel(string fileName, ItemType itemType, IEnumerable<ExportableToExcelItem> items)
    {
        this.FileName = fileName;
        this.ItemType = ItemType;
        this.Items = items;
    }

    public string FileName { get; set; }
    public ItemType ItemType { get; set; }
    public IEnumerable<ExportableToExcelItem> Items { get; set; }

}

Thanks in advance!

First time i've ever needed to actually ask a question here since every other question i've ever had i've found already answered here :)


EDIT: Posting FormCollection results:

http://imageshack.us/photo/my-images/861/sinttulonsa.png Sorry, newbies cant post pics :(


Solution

  • The reason is that a RedirectToAction result will launch a GET request and your parameters will have to be passed along through the querystring. Obviously there is a limit to the amount of characters a url can consist of.

    Seems to me that you should do the conversion to Excel in a class instead of behind another Action.

    So CustomExportAction1 and CustomExportAction2 both call

    return File(ExcelExporter.ExportExcel(dataToExport));
    

    or something similar.