I am trying to send all gridview records to webmethod using jquery ajax but it is not working. Here is my code
我正在尝试使用jquery ajax将所有的gridview记录发送到webmethod,但它不起作用。这是我的代码
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....
帮我家伙....
1 个解决方案
#1
3
That must be throwing a 500 inetrnal server
error because of type mismatch:-
由于类型不匹配,必须抛出一个500 inetrnal服务器错误:-
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.
您正在传递一个JSON字符串,因此您应该期望在服务器端得到相同的结果,然后将其反序列化为. net对象。
Update:
更新:
You can use the JavaScriptSerializer class:-
您可以使用JavaScriptSerializer类:-
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; }
}
#1
3
That must be throwing a 500 inetrnal server
error because of type mismatch:-
由于类型不匹配,必须抛出一个500 inetrnal服务器错误:-
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.
您正在传递一个JSON字符串,因此您应该期望在服务器端得到相同的结果,然后将其反序列化为. net对象。
Update:
更新:
You can use the JavaScriptSerializer class:-
您可以使用JavaScriptSerializer类:-
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; }
}