You must specify that it is an array like this and pass each value as a parameter, only all with the same name, for example:
/?arr[]=foo+bar&arr[]=baz&arr[]=foo
Or still for some cases (such as ASP MVC):
/?arr=foo+bar&arr=baz&arr=foo
As demonstrated in that and in that topic of SO-EN and in that post
So in your case you should do something like:
window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro=1&GarsFiltro=2&GarsFiltro=3"
To generate this parameter dynamically I created this method:
function formatQueryStringURLParamArray(key, array){
var param = "";
for(var item in array){
if(param.length > 0)
param += "&";
param += key + "=" + item;
}
return param;
}
Which can be called this way by returning the formatted querystring:
var param = formatQueryStringURLParamArray("key", myArray);
Here’s a online example.
Example with Web API
Try the following test (I use Webapi ASP MVC, but it is very similar to MVC):
I created the following Apicontroller:
public class Test2Controller : ApiController
{
[HttpGet]
public virtual int Get([FromUri]int[] i)
{
return i.Length;
}
}
And I made the following request via url in my browser:
http://localhost:59402/api/test2/?i[]=1&i[]=2&i[]=3
And I got the integer array correctly.
I don’t know if MVC uses this because I don’t work with MVC just Webapi, but try adding [FromUri]
before its array parameter in the Controller method.
Example with ASP MVC
I created this Controller:
public class Test3Controller : Controller
{
[System.Web.Http.HttpGet]
public ActionResult Index([FromUri]int[] i)
{
return Json(i, JsonRequestBehavior.AllowGet);
}
}
And I made the following request via url in my browser:
http://localhost:59402/test3/?i=1&i=2&i=3
Note: I don’t know why MVC didn’t understand when I was passing by i[]
, so if I just pass i={valor}
, he understands. Already Web API worked both ways: i={valor}
and i[]={valor}
.
You must specify that it is an array like this:
window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro[]=" + myArray +""
, at a glance at that link of SO-EN and in that who deal with this subject.– Fernando Leal
Yeah, and it makes sense to use
GarsFiltro[]=" + myArray +""
, but I still getnull
in the same– CesarMiguel
OK. is that I forgot a detail I will post as reply if I do not resolve I remove.
– Fernando Leal
Are you not using Jquery? You would have more ease and control by making an ajax call.
– lionbtt
Yes @lionbtt, I’m using jquery. I ended up doing it with javascript, but I can also try it with ajax. Although I don’t think the problem is from there
– CesarMiguel