0

I would like to pass an array of IDs to a controller. Originally I was adding each ID in the query string like so:

http://localhost:4000/customers/active?customerId=1&customerId=2&customerId=3

Then on the controller side I had a method that would accept the array like this:

GetCustomers([FromQuery] int[] ids)
{
   ...
}

This was working well but there are a few situations where there are so many customerIds in the array that the query string became too long so I had to modify the way that the query was being passed to this:

http://localhost:4000/customers/active?customerIds=1,2,3

I got the solution working by changing GetCustomers params to accept a string instead of an int array and then parsed the customerIds out in the controller (using .Split(','))

I feel like it was cleaner to pass an array directly instead of having to modify the string on the server side. Is there a way to achieve this given the way the customerIds are now being passed?

Turner Bass
  • 801
  • 8
  • 20
cooper
  • 417
  • 1
  • 10
  • 20

3 Answers3

2

1. USE POST

2. USE AJAX & SEND DATA AS JSON

 $.ajax({
           type: "POST",
           url: "/Home/GetCustomers",
           data : { stringOfCustomerIds : JSON.stringify(arrCustomerIds)},
           dataType: "json",
           success: function (response) {
                 //do something with the response
           }

& on the controller side

public JsonResult GetCustomers(string stringOfCustomerIds )
{
     JObject CustomerIdsJson = JObject.Parse(listOfCustomerIds );

       foreach (JProperty property in CustomerIdsJson .Properties())
       {
           Console.WriteLine(property.ID+ " - " + property.Value);
       }

      return Json(output, JsonRequestBehavior.AllowGet);  

}
Rohit Kumar
  • 1,777
  • 2
  • 13
  • 26
  • If you going to do a POST, then it would be just `data : JSON.stringify({ stringOfCustomerIds : arrCustomerIds }),` with `contentType: 'application/json'` and the method would be `public JsonResult GetCustomers(int[] stringOfCustomerIds)` (let the `ModelBinder` do its work) –  Oct 19 '17 at 22:24
1

Based on your use of the [FromQuery] attribute, I can tell that you are using .NET Core (which this only applies to). [FromQuery] has no way of knowing which part of the query string you want to map to the parameter, so you have to provide a Name parameter like this:

[FromQuery(Name ="ids")]

The Name parameter can have whatever value you'd like. Just as long as your query matches the name you select. So for the example above:

?ids=2&ids=3&ids=4 but if you were to formulate the attribute like

[FromQuery(Name = "kittens")] then you would need to make your query look like

?kittens=2&kittens=3&kittens=4

Following this methodology you'll be able to see that your parameter is populated correctly.

Turner Bass
  • 801
  • 8
  • 20
0

You could pass the IDs as a JSON object in the body of the message using a POST request on the front end and the [FromBody] tag on the back end controller. This way your url will simply look like this: http://localhost:4000/customers/active no matter how many IDs are present in the body of the message. It also saves you the hassle of extracting and pushing each parameter into a new array element.

Mish
  • 145
  • 5