About me

My name is Marko Markovic and I am a software developer, a musician and a researcher. I am intrigued by algorithms and new techniques and I want to improve my software development skills.

While working on different projects I like to document what I have learned. A new technique, a new algorithm, solution to a problem or a tool. The reason I am writing this blog is to help out other people in their programming endeavors and also I write so I won't forget.

If you are interested to find out more about me visit markomarks.com

Sunday, May 12, 2013

asp.net pass int list to a controller

Howdy guys, another rainy day in Belgrade and I've decided to share some of the last weeks thoughts with you guys and girls. This one is useful have to say, considering the waste of time the problem can cause.

So, without further due, let's dig into that.

Situation is quite simple, we need to redirect to another page or a controller and we need to pass an int list to it as an parameter.


So what exactly is the problem here? Well, ASP.NET MVC in particular doesn't recognize arrays or lists as parameter that easily. You cannot do it the lazy way, just pass a list variable as a parameter in RedirectToAction method. On the post action, if you catch it in debug, you would see that in most cases that parameter ends up as a NULL. Maybe forcing a Request.Params["paramName"] in the "receiver" method and then additional parsing of the data would work, but I haven't tried that yet, and not that I want to, because it seems a bit too much work for that kind of task.

I don't know why passing a parameter list is so troublesome, maybe it could be Microsoft's oversight, or I just don't think the way they intended for the RedirectToAction method to be used. My expectation is that if I am passing an integer list to that method, I expect for it to show up on the "other side".
But anyway, let us get back to the solution. If you want to pass a integer list using the RedirectToAction method, check out the example bellow.
var parementerIntList= new List<int>();

parementerIntList.Add(1);
parementerIntList.Add(2);
parementerIntList.Add(3);

Response.RedirectToAction("Receiver", "ControllerName", new { intList = string.Join("&intList =", parementerIntList) });

I know, you are not mistaken. I am passing an string to the controller. Why? Because it works. Does it make any semantic sense? Well, no. At least not to me.
The url itself would look something like host/controllername/receiver?intList=1&intList=2&intList=3. Ugly and clogs up the URL, but it works. Imagine a list with 20 parameters or something similar, that would be fun, don't you think?
On the receiver side, you will finally get what you have wanted :).

public ActionResult Receiver(List<int> intList)
{
   /* breakdance */
   return View();
}

That is it for me today, I know the solution isn't fabulous or awesome but it gets the job done. If you by any chance have a better suggestion for the solution, please let me know.

Until next time,
Be safe

No comments:

Post a Comment

Blog Archive