Andy Donegan Andy Donegan - 5 months ago 12x
Javascript Question

MVC Call new Action and View from Javascript and send Array

I currently have an IEnumerable view in a grid which I have added check boxes, to allow me to select items (which I grab the id's via JavaScript and build an array of just the id's).

I have a button which should send the array to a new action and load the corresponding view.

I attempted at first to send the array via a JSON object which worked, but I could not get the controller to load a new view, it just returns the results to the current view (not what I am wanting).

So I am now just trying the following :

window.location.href = '/TeamSelector/TeamList?array=' +SwimmerList;

This is working but the object is not being sent correctly as I receive nothing in the array at the controller end.

public ActionResult TeamList (List<int> Array)
// Do something here
return view(results)

Could somebody please advise me on how to structure the JavaScript code to send the array so that it will be accepted by the controller.

Any suggestion/advice would be greatly appreciated.



In order to post back a collection of values to a GET method with the signature

public ActionResult TeamList (List<int> Array)

your url needs to be

/TeamSelector/TeamList?array=1&array=2&array=3 // etc

One way to achieve this would be (assumes your checkboxes have class="checkbox")

location.href = '/TeamSelector/TeamList?array=' + $('.checkbox:checked').map(function () {
    return $(this).val();