3

I am trying to send all gridview records to webmethod using jquery ajax but it is not working. Here is my code

function Save() {
            var TableData = new Array();

            $('[id*=GridView1] tr').each(function (row, tr) {
                TableData[row] = {
                    "Sr"    : $(tr).find('td:eq(0)').text()
                  , "RollNo": $(tr).find('.RollNo').val()
                  , "Name"  : $(tr).find('.Name').val()
                  , "Marks" : $(tr).find('.Marks').val()
                }
            });
                        TableData.shift();
            $.ajax({
                type: "POST",
                url: "TestPage.aspx/SaveData",
                data: "{Data:'" + JSON.stringify(TableData) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(msg.d);
                }
            });
            return false;
        }

and Code Behind

  [WebMethod]    
    public static string SaveData(List<string> Data)
    {      
       //My Code
        return "Success";
    }

Help me guys....

7
  • Does the control hit the webmethod?? Did you debug? Commented Oct 8, 2015 at 7:39
  • what is the issue? as i see you have a dataType: "json", and you are not returning back a json response. so i can suggest you to remove it or change it to dataType: "text", Commented Oct 8, 2015 at 7:44
  • I debug the code. it's not calling webmethod. Commented Oct 8, 2015 at 7:45
  • also why TableData.shift();? Commented Oct 8, 2015 at 7:45
  • Hi @Jai I used dataType: "text", but it's still not working. TableData.shift(); is used to remove the header row. Commented Oct 8, 2015 at 7:48

1 Answer 1

3

That must be throwing a 500 inetrnal server error because of type mismatch:-

 public static string SaveData(string Data)
    {      
       //My Code
        return "Success";
    }

You are passing a JSON string so you should expect the same at server side and then deserialize it into a .Net object.

Update:

You can use the JavaScriptSerializer class:-

public static string SaveData(string Data)
{      
   JavaScriptSerializer json = new JavaScriptSerializer();
   List<GridData> mygridData = json.Deserialize<List<GridData>>(Data);
   return "Success";
}

You are not passing a List<String> first of all from client side, you are passing a javascript object with properties. So to map it in .Net you will have to define an equivalent Type like this:-

public class GridData
{
    public string Sr{ get; set; }
    public string RollNo{ get; set; }
    public string Name{ get; set; }
    public string Marks{ get; set; }        
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Rahul Singh. I want to receive an array in server side. So how can I deserialize it into a .Net object ?
@ShaiwalTripathi - It's a class which will store data at server side, I have updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.