My problem is exactly this : How To Pass a MultiDimensional Array from Javascript to server using PageMethods in ASP.Net
I am trying to do same thing but the solution doesn't work for me and i'm confused. He says JSON.stringify
works but it makes my object array a entire string
. I can't use string[,]
or List<string>
for parameter type. If i don't use JSON.stringify
i can pass data with List<object>
type. But i couldn't get values in c#. So i need some help. Basically; i need to pass my multidimensional array to c# and i must get values with in loop. So need some advice. Here's my code:
Jquery:
var fixedQuestions = $(".questId").map(function () {
return { group:$(this).closest('li').attr('id') , id:$(this).text() };
}).get();
var test1 = JSON.stringify(fixedQuestions);
PageMethods.InsertAuditParams(test1);
and here is my c# Web Method:
[WebMethod]
public static void InsertAuditParams(string[,] testarray)
{
for(int i = 0; i < testarray.Length; i++)
{
string value1 = testarray[i, 0].ToString();
}
}
and this is the error that i get on firebug console:
...Cannot convert object of type 'System.String' to type 'System.String[,]'
Thanks in advance.
I've solved it. It was about passed collection type. It was look like dictionary
type at c# and that's why i couldn't get values. So i created a js function for manupulate the data as i want. Here is the solution for new comers;
function ConvertToArray(objectData)
{
var multiArray = [];
for (var key in objectData) { multiArray.push([objectData[key].num,
objectData[key].text]); }
return multiArray;
}
var passFixed = ConvertToArray(fixedQuestions);
PageMethods.InsertAuditParams(passFixed);
and this is how to get values at c#:
public static void InsertAuditParams(List<object> fixed_q)
{
foreach (object[] item in fixed_q)
{
string value1 = item[0].ToString();
string value2 = item[1].ToString();
}
}