2

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.

Andy.

6
  • 1
    It would need to be /TeamSelector/TeamList?array=1&array=2&array=3 etc Commented May 3, 2016 at 11:12
  • @StephenMuecke thank you very much for the quick answer. I have just tested and it worked first time. I am going to write a little bit of code to iterate through the array and add the text to the url (this may take me 3 weeks and 4 more SO questions). Stephen could you post as the answer please so I can mark it, another question you have resolved for me and it is very appreciated. Commented May 3, 2016 at 11:25
  • If you give me 30 min, I'll also show you how to generate it with the values from the javascript array :) Commented May 3, 2016 at 11:27
  • @StephenMuecke, no please don't I will figure it out and your time is much better spent helping the likes of me out when we get totally stuck !! I have the basics in my mind should not take me too long :) Commented May 3, 2016 at 11:28
  • OK done, but I'll update it later anyway :) Commented May 3, 2016 at 11:34

2 Answers 2

2

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();
}).get().join('&array=');
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately I have persisted down the route of using Grid.MVC which I have found with time is very limiting and a pretty dead plugin. The checkbox functionality within this is strange and shows two check boxes and I can not seem to update them, so I add the click function to manually build up my array. Your elegant solution would be perfect with out Grid MVC.
1

Thanks to Stephen and his answer, I have written a basic script to build the url with array data and all is working now.

        var url = '/TeamSelector/TeamList?';
    for (var i = 0; i < SwimmerList.length; i++)
    {
        if (i == 0) {
            url = url + 'array=' + SwimmerList[i];
        } else {
            url = url + '&array=' + SwimmerList[i];
        }
    }
    window.location.href = url;

Due to my using Grid.MVC I have struggled with the checkbox and getting the status of them so I ended up adding an on click to the checkbox and I control it that way to build my array up.

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.