7

I have to pass two dimensional array to the page method written at code behind of the web page in asp.net I have a variable objList as a two dimensional array. I used following code for achieving this with no success and page method is not called.

JAVASCRIPT

function BindTable(objList) {

    $.ajax(
    {
           url: "CompCommonQues.aspx/SaveData",
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           type: "POST",
           data: { data: objList },
           success: function (data) {
           //Success code here
    },
    error: function () { }
    });
  }

CODE BEHIND .CS File

 [WebMethod]
public static string SaveData(string[,] data)
{
    string[,] mystring = data;
    return "saved";
}

There is method like JSON.stringify(objList) to pass the json array to code behind but couldn't implement this. A simple call wihtout array working for me like

data: "{ 'data':'this is string' }",

and in code behind

[WebMethod]
public static string SaveData(string data)
{
    string mystring = data;
    return "saved";
}

There is problem in passing data. Can you assist me how to pass it for arrays?

1 Answer 1

7

Try the correct JSON notation in JavaScript

var objList = new Array();
objList.push(new Array("a","b"));
objList.push(new Array("a", "b"));
objList.push(new Array("a", "b"));

   $.ajax({
       type: "POST",
       url: "copyproduct.aspx/SaveDate",
       data: "{'data':'" + JSON.stringify(objList) + "'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (msg) {
            alert(msg.d);
       }
   });

In code behind, you can deserialize with JavaScriptSerializer (System.Web.Script.Serialization)

[WebMethod()]
public static string SaveDate(string data)
{
    JavaScriptSerializer json = new JavaScriptSerializer();
    List<string[]> mystring = json.Deserialize<List<string[]>>(data);
    return "saved";
}

I had to deserialize to a generic list of a string array because you can't deserialize to a string (check: http://forums.asp.net/t/1713640.aspx/1)

Sign up to request clarification or add additional context in comments.

11 Comments

Web method is not hitting when i debug the code. What changes required in code behind function.
You are passing a string like: { data: [["a","b"],["a","b"],["a","b"]] }. So you need to change the parameter from string(,) to string and use System.Web.Script.Serialization.JavaScriptSerializer to deserialize to string(,). I will work out the example.
I have change my code as per your suggestion, Arrays are created but code behind method still not hitting.
Did you copied the code? Are you using Firebug? Can you see what message is displayed under "Net"> "XHR"? I have tested it in my project and it was working.
Error is internal server error Under the post section { data: [["123","1"],["153","2"],["123","1"],["123","1"],["153","2"],["123","1"],["123","1"],["153","2"],["123","1"]] } and Message is "Type 'System.String' is not supported for deserialization of an array."
|

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.