RedirectToAction is not a switch to another controller method. This is a return to the client (browser) of a new url, which leads to another Action. What you pass to the third parameter becomes part of this url.
Your sheet is assigned to the returned link as a GET parameter. The link is given to the client side - the browser. The browser makes a new request for the link. The server is trying to get data from the incoming request for the myList parameter. And then a couple of problems arise.
ASP.NET MVC does not know how to convert lists and generally complicated things into url parameters. It is limited to calling ToString. Therefore, the client is given a not very useful link like
/ListManager/ListViewer?myList=System.Collections.Generic.List%601%5BSystem.Int32%5D
When processing a request, ASP.NET MVC can parse the List parameter. But only if each of the elements of the list comes in a separate GET parameter. With the names myList[0] ..., myList[1] . Well, or at least [0] , [1] . Those. he is waiting for a reference
/ListManager/ListViewer?%5B0%5D=1&%5B1%5D=2&%5B2%5D=3
To generate it, your code should look something like this:
var values = new RouteValueDictionary( list .Select((item, index) => new { item, index }) .ToDictionary( key => string.Format("[{0}]", key.index), value => (object)value.item ) ); return RedirectToAction("ListViewer", "ListManager", values);
But in your case - these are all unnecessary crutches. There are two much simpler options:
- Correct - to take out the general code from two actions in a separate method. Return from both your action and ListViewer the result of calling this method. And do not redirect the client back and forth, with the data in the address bar.
- Slightly less correct is to redirect to ListViewer without parameters. Well, or with a parameter like
fromSession = true . ListViewer is the same Action as the current. It can also get values from a session in the same way. There is no need to transfer them as part of the url to the client side, and then back.