admin管理员组

文章数量:1314843

I have my array populated like so:

updateLabels: function () {
    var diagrams = _stage.diagramLayer.getChildren();

    var ponentIDs = new Array();

    for (var index = 0; index < diagrams.length; index++) {
        ponentIDs.push(diagrams[index]ponentID);
    }

    var self = this;
    $.ajax({
        url: '../PlanView/UpdateDiagrams',
        type: 'POST',
        data: { ComponentIDs: JSON.stringify(ponentIDs), 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> ponentIDs, string rackInfo)
{
    List<object> diagramInformation = new List<object>(ponentIDs.Count());
}

my data as it is being passed across 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 change UpdateDiagrams to expect List<string> then I get a list with one item in it, the entire ComponentIDs string.

What am I doing incorrectly here?

EDIT: I am working under MVC3. I should be able to leverage some sort of automatic deserialization when passing to my controller, I am just not sure how.

SOLUTION: The solution was to wrap my data object in JSON.stringify, not just ponentIDs. Even though I can get the RackInfo variable server-side without converting it to JSON.

I have my array populated like so:

updateLabels: function () {
    var diagrams = _stage.diagramLayer.getChildren();

    var ponentIDs = new Array();

    for (var index = 0; index < diagrams.length; index++) {
        ponentIDs.push(diagrams[index].ponentID);
    }

    var self = this;
    $.ajax({
        url: '../PlanView/UpdateDiagrams',
        type: 'POST',
        data: { ComponentIDs: JSON.stringify(ponentIDs), 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> ponentIDs, string rackInfo)
{
    List<object> diagramInformation = new List<object>(ponentIDs.Count());
}

my data as it is being passed across 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 change UpdateDiagrams to expect List<string> then I get a list with one item in it, the entire ComponentIDs string.

What am I doing incorrectly here?

EDIT: I am working under MVC3. I should be able to leverage some sort of automatic deserialization when passing to my controller, I am just not sure how.

SOLUTION: The solution was to wrap my data object in JSON.stringify, not just ponentIDs. Even though I can get the RackInfo variable server-side without converting it to JSON.

Share Improve this question edited Mar 21, 2012 at 15:33 Sean Anderson asked Mar 20, 2012 at 23:27 Sean AndersonSean Anderson 29.4k33 gold badges132 silver badges242 bronze badges 1
  • Maybe ponentIDs.push(parseInt(diagrams[index].ponentID));? – MiMo Commented Mar 20, 2012 at 23:41
Add a ment  | 

3 Answers 3

Reset to default 4

If you want your posted data to be in JSON format then try something like this. MVC should then be able to deserialise it automatically on the server-side.

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

(I'm unable to test it at the moment, but it should hopefully be along the right lines even if it's not pletely bug-free.)

You're sending a string that contains a list of strings. When it gets to the server, the string needs to be deserialized.

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

I changed the parameter to a string. When you had it as a list of int, it was an empty list since you were not passing a list of ints.

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

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

So that the data doesn't include the brackets [].

Let me know how this works.

I haven't been able to test it with ASP.NET MVC, but if you remove the JSON.stringify everything should work correctly. This is the Form Data without JSON.stringify:

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

That's the normal way an array is posted to the server.

With JSON.stringify:

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

The call to JSON.stringify converts the array to the string "[10, 20, 30]" so you're posting a string to the controller.

本文标签: