Reputation: 627
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....
Upvotes: 3
Views: 3470
Reputation: 21795
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; }
}
Upvotes: 3