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

StackOverflow https://stackoverflow.com/questions/8535962

Question

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 :(

Était-ce utile?

La 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.

Autres conseils

try to switch your ExportToExcel signature to

public ActionResult ExportToExcel(FormCollection data)
{
  var model = new ExportToExcelModel();
  try
  {
    UpdateModel(model, data)
  }
  catch(UpdateModelException ex)
  {
  }
}

look at what's in the FormCollection (that might help), and also see if UpdateModel is throwing an exception, because this is what is happening behind the seen when you make your action method take in a model instead of a FormCollection.

Hope that help you track it down

UPDATE: You might have to do it using TempData, read this, supposedly you can't do this out of the box with ASP.NET MVC!!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top