Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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....

share|improve this question
    
Does the control hit the webmethod?? Did you debug? – Guruprasad Rao Oct 8 '15 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", – Jai Oct 8 '15 at 7:44
    
I debug the code. it's not calling webmethod. – Shaiwal Tripathi Oct 8 '15 at 7:45
    
also why TableData.shift();? – Jai Oct 8 '15 at 7:45
    
Hi @Jai I used dataType: "text", but it's still not working. TableData.shift(); is used to remove the header row. – Shaiwal Tripathi Oct 8 '15 at 7:48
up vote 2 down vote accepted

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; }        
}
share|improve this answer
    
Hi @Rahul Singh. I want to receive an array in server side. So how can I deserialize it into a .Net object ? – Shaiwal Tripathi Oct 8 '15 at 7:59
    
@ShaiwalTripathi - Check my update. – Rahul Singh Oct 8 '15 at 8:04
    
What is GridData ? – Shaiwal Tripathi Oct 8 '15 at 8:08
    
@ShaiwalTripathi - It's a class which will store data at server side, I have updated. – Rahul Singh Oct 8 '15 at 8:09
1  
It's working fine now. Thank you very much – Shaiwal Tripathi Oct 8 '15 at 8:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.