Pass an int list from JavaScript to C # - I get a list, but it's empty; Are these forms not structured properly?

I have an array filled like this:

updateLabels: function () { var diagrams = _stage.diagramLayer.getChildren(); var componentIDs = new Array(); for (var index = 0; index < diagrams.length; index++) { componentIDs.push(diagrams[index].componentID); } var self = this; $.ajax({ url: '../PlanView/UpdateDiagrams', type: 'POST', data: { ComponentIDs: JSON.stringify(componentIDs), RackInfo: $('#RackInfoSelect').val() }, success: function (data) { console.log('success'); }, error: function () { console.log("error"); } }); }, 

server side I have this method:

 [CompressionFilterAttribute] public JsonResult UpdateDiagrams(List<int> componentIDs, string rackInfo) { List<object> diagramInformation = new List<object>(componentIDs.Count()); } 

my data when it is transmitted over the network:

 ComponentIDs:[74,445,732,351,348,347,1123,599,600,1053,350,601,602,603,332,99,877,919,349,348,347,347,349,348] RackInfo:Equipment Weight 

I successfully get RackInfo, if I modify UpdateDiagrams to expect a List<string> , then I get a list with one element in it, the entire string of ComponentIDs.

What am I doing wrong here?

EDIT: I work under MVC3. I should be able to use some automatic deserialization when moving to my controller, I'm just not sure how to do this.

SOLUTION: the solution was to wrap my data object in JSON.stringify, and not just the component identifiers. Although I can get the RackInfo variable on the server side without converting it to JSON.

+6
source share
3 answers

If you want your published data to be in JSON format, try something like this. Then MVC should be able to deserialize it automatically on the server side.

 $.ajax({ url: '../PlanView/UpdateDiagrams', type: 'POST', contentType: 'application/json', data: JSON.stringify({ componentIDs: componentIDs, rackInfo: $('#RackInfoSelect').val() }), success: function (data) { console.log('success'); }, error: function () { console.log("error"); } }); 

(I cannot check it at the moment, but he should hope that he will stand in the right line, even if he will not be completely error-free.)

+4
source

You are sending a string containing a list of strings. When it hits the server, the string should be deserialized.

 [CompressionFilterAttribute] public JsonResult UpdateDiagrams(string ListcomponentIDs, string rackInfo) { List<int> componentIDs = (from string s in ListcomponentIDs.Split(',') select Convert.ToInt32(s)).ToList<int>(); } 

I changed the parameter to a string. When you used it as an int list, it was an empty list because you did not pass the ints list.

Also, in JS, you don't need to serialize the array, just call ToString on it:

 data: { ComponentIDs: componentIDs.toString() ... 

So the data does not contain brackets [].

Let me know how it works.

+1
source

I could not verify it using ASP.NET MVC, but if you remove JSON.stringify, everything should work correctly. This is form data without JSON.stringify:

  • ComponentIDs []: 10
  • ComponentIDs []: 20
  • ComponentIDs []: 30
  • RackInfo: Equipment Weight

This is the usual way to send an array to the server.

With JSON.stringify:

  • ComponentIDs: [10,20,30]
  • RackInfo: Rackinfo

Calling JSON.stringify converts the array to the string "[10, 20, 30]", so you send the string to the controller.

0
source

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


All Articles