Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in asp.net mvc, how can I pass an array of integers as a parameter

i have a controller function that previously had integers as each section in the URL (which i setup in the routing file) but now one of the parameters needs to be an array of integers. Here is the controller action:

    public JsonResult Refresh(string scope, int[] scopeId)
    {
        return RefreshMe(scope, scopeId);
    }

in my javascript, i had the below but i now need to get scopeId to be an integer array. how can i setup up a url to post to using jquery, javascript

   var scope = "Test";
   var scopeId = 3;

  // SCOPEID now needs to be an array of integers

  $.post('/Calendar/Refresh/' + scope + '/' + scopeId, function (data) {
        $(replacementHTML).html(data);
        $(blockSection).unblock();
  }
like image 475
leora Avatar asked Apr 26 '11 02:04

leora


1 Answers

The following should do the job:

var scope = 'Test';
var scopeId = [1, 2, 3];

$.ajax({
    url: '@Url.Action("Refresh", "Calendar")',
    type: 'POST',
    data: { scope: scope, scopeId: scopeId },
    traditional: true,
    success: function(result) {
        // ...
    }
});

and if you are using ASP.NET MVC 3 you could also send the request as a JSON object:

$.ajax({
    url: '@Url.Action("Refresh", "Calendar")',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ scope: scope, scopeId: scopeId }),
    success: function(result) {
        // ...
    }
});
like image 77
Darin Dimitrov Avatar answered Oct 07 '22 09:10

Darin Dimitrov