简体   繁体   中英

Passing an array to an MVC controller

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?

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);  

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM