0

Is this possible?
What I want to do is pass a list of objects as a paramter in an actionlink
At the moment when I try to do this the list is always empty by the time it reaches the controller!

In the view

 <%= Url.Action("ActionName", new { list = Model.ListOfObjects}) %>

In the controller

public ActionResult ActionName(List<Object> list)
    {
        //Do stuff
    }
2
  • /Controller/Object?list=System.Collections.Generic.List%601%5BObject%5D Commented Mar 26, 2010 at 12:21
  • Sorry that should be /Controller/ActionName?list=System.Collections.Generic.List%601%5BObject%5D Commented Mar 26, 2010 at 12:23

3 Answers 3

1

In terms of whether or not it's possible - it's possible, but not in the way you're trying. Keep in mind that this will translate to a URL which will get parsed by MVC and the different parameters will get passed to the action either as direct parameters or through a model binder.

I would recommend that you try to figure out what the URL will have to look like and then maybe do some custom code to generate the URL (maybe use a custom helper function/extension method). If you combine this with a custom model binder you should have a pretty elegant solution which does exactly what you want.

For example, if your list has 3 objects of type string you could write a helper to generate a url like this (let's say the list contains 'first', 'second', and 'third')

/Controller/Action?obj1=first&obj2=second&obj3=third

Now you simply need to write a model binder that looks for entries called 'obj1','obj2', etc and simply add the results into a list.

Sign up to request clarification or add additional context in comments.

Comments

0

While it may take the list of objects, it probably won't render what you are expecting to see; it may actually suck in the values of the list class (or whatever class this list represents) and use that as the params.

Not through a get request, unless you write out the params manually and deserialize the params too. Or, instead, do a postback to the server with these values within the form; then, this would populate correctly.

HTH.

Comments

0

Something like this would work, I suggest making a helper to contain this logic if you need it in a lot of places.

<% var values = new RouteValueDictionary();
   for (int i = 0; i < Model.ListOfObjects.Count -1; i++)
   {
      values.Add("list["+i+"]", Model.ListOfObjects[i]);
   }%>
<%= Url.Action("ActionName", values); %>

The action will know how to handle the list in the controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.