Question

I am passing a generic list of objects from a view page to an action method via an action link. The action link looks like this

@Html.ActionLink("Click here", "Foo", new { a = Model.A }) 

The action method signature is

public ActionResult Foo(List<foo> a)

The problem is that when I run this code in the debugger I can clearly see that there are two elements in this List in the view but when the execution reaches the action method there are no elements in the list. Why might this be?

It's been suggested that I "post to ActionResult". What does this mean? If it is the solution then great.

Was it helpful?

Solution

You could try this, which will pass the elements as ?a[0]=foo&a[1]=bar&a[2]=hello

@{
    var dict = new RouteValueDictionary();
    for (int i = 0; i < Model.A.Count; i++)
    {
        dict.Add("a[" + i + "]", Model.A[i]);
    }
}
@Html.ActionLink("Click here", "Foo", dict);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top