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

I have a controller function that previously had integers in each section in the URL (which I configure in the routing file), but now one of the parameters should 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 below, but now I need to get the scopeId for the integer array. how can I configure the url to publish 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(); } 
+6
source share
2 answers

The following should complete the following task:

 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 can 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) { // ... } }); 
+12
source

Here is an example from the jQuery API for $ .post ()

Example: transfer data arrays to the server (while ignoring the return results anyway).

 $.post("test.php", { 'choices[]': ["Jon", "Susan"] }, function() { //do stuff }); 
0
source

Source: https://habr.com/ru/post/886676/


All Articles