0

I am passing multidimensional array in web method as object type. I want to convert this object type to multiple array in the method.

The method, which does the conversion:

[WebMethod]
public static string Save(object arr)
{
  Dictionary<string, object> value = (Dictionary<string, object>)arr;
  return ""; 
}

The client-side code:

//ajax method
var arr=new Array();

 var table = document.getElementById('table');
    for (var r = 0, n = table.rows.length; r < n; r++) {
        arr[r] = new Array(10);
        for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
            arr[r][c]=table.rows[r].cells[c].innerHTML;
        }
    }
    console.log(arr);
  $.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    url: "salary.aspx/Save",
    data: JSON.stringify({ arr: arr }),
    success: function (response) {
        alert(response.d);            
    }
});

The error message is:

Unable to cast object of type 'System.Object[]' to type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]'.

1
  • You wont be able to directly cast it. Try looping and iterating over individual items. Commented Apr 28, 2015 at 10:50

1 Answer 1

1

First of all you need to create your inner array differently:

//ajax method
var arr=new Array();

 var table = document.getElementById('table');
    for (var r = 0, n = table.rows.length; r < n; r++) {
        arr[r] = {};
        for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
            arr[r][c]=table.rows[r].cells[c].innerHTML;
        }
    }
    console.log(arr);
  $.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    url: "salary.aspx/Save",
    data: JSON.stringify({ arr: arr }),
    success: function (response) {
        alert(response.d);            
    }
});

Then you should be able to do something like this:

[WebMethod]
public static string Save(object arr)
{
    object[] table = (object[])arr;

    // now the the object contains all your row values as an object.

    return ""; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

This code is untested, but the stringify should be putting a single dimensional array down with each row represented as an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.