MVC3 - Unable to pass int [] to controller action on RedicrectToAction from another action

I have 2 actions inside one 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 go over, the checked Records parameter is correctly populated in the RedirectToAction statement, but when it gets into the Index ActionResult, checkedRecords is NULL. I tried doing filter =, checkedRecords = etc. I have no problem with this from View to Controller. If I changed the type of the array to something else, I can capture the value - how do I pass int [] from action to action? What am I doing wrong? Thanks you

+4
source share
3 answers

You cannot send complex types as redirection parameters in MVC, only primitive types such as numeric and string values

Use TempData to pass an 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 } 
+6
source

You send two null values. When you use the new {}, you create a new object. You must not only define index names, but also values.

 return RedirectToAction("Index", new { filter = filter, checkedRecords = checkedRecords }); 
+1
source

I'm not sure ASP.Net knows how to create a url using the int array you pass in. If an int array uniquely identifies a resource, you can try converting the array to a delimited string (or similar) separated by hyphens, and then parse the string in the index method.

If you are just trying to save data between requests, use TempData:

http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.tempdata.aspx

+1
source

Source: https://habr.com/ru/post/1413445/


All Articles