Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 - Having trouble passing int[] to controller action on RedicrectToAction from a different action

I have 2 actions within the same controller.

public ActionResult Index(string filter, int[] checkedRecords)

and

public ActionResult ExportChkedCSV(string filter, int[] checkedRecords)

The second Action (ExportChkedCSV) contains this redirect:

if (reject != 0)
        {
            return RedirectToAction("Index", new { filter, checkedRecords });
        }

When I step through, the parameter checkedRecords is populated correctly on the RedirectToAction statement, but when it hits the Index ActionResult from there, checkedRecords is null. I've tried doing filter =, checkedRecords =, etc. I am having no problem with this from View to Controller. If I change the array type to anything else, I can grab the value - how do I pass int[] from action to action? What am I doing wrong? Thank you

like image 937
user1166147 Avatar asked Feb 21 '23 10:02

user1166147


2 Answers

You can't send complex types as redirect parameters in MVC, only primitive types like numerics and strings

Use TempData to pass the array

...
if (reject != 0) {
    TempData["CheckedRecords"] = yourArray;
    return RedirectToAction("Index", new { filter = filterValue });
}
...

public ActionResult Index(string filter) {
    int[] newArrayVariable;
    if(TempData["CheckedRecords"] != null) {
        newArrayVariable = (int[])TempData["CheckedRecords"];
    }
    //rest of your code here
}
like image 170
CD Smith Avatar answered May 26 '23 12:05

CD Smith


You are sending two null values. When you use new {} you are making a new object. You have to not only define the index names, but the values as well.

return RedirectToAction("Index", new { filter = filter, checkedRecords = checkedRecords });
like image 23
Travis J Avatar answered May 26 '23 11:05

Travis J