Reputation: 403
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]'.
Upvotes: 0
Views: 395
Reputation: 1422
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 "";
}
Upvotes: 1