ajax给webmethod方法传递json数组,C#接收

前端之家收集整理的这篇文章主要介绍了ajax给webmethod方法传递json数组,C#接收前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@javascript代码段:

@H_301_0@//<!--提交数据-->
function btsubmit_click() {
var data = "{'record':[";
//循环生成data传到后台
$("." + tby + tday).not(":first").each(function () {
data = data + "{'id':'" + $(this).prevAll(".itemd").find(".hid_id").val() + "','value':'" + $(this).find(".txt" + tday).val() + "'},";
if ($(this).prevAll(".target").text() == "") {
return false;
}
})
data = data.substring(0,data.length - 1) + "]}";
$.ajax({
type: "Post",
url: "Asset.aspx/submits",
data: data,
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (result) {
alert(result.d);
},
error: function (e) {
alert(e.responseText);
}

@H_301_0@ })
}

@H_301_0@

@H_301_0@C#代码

@H_301_0@[WebMethod]
public static string submits(Dictionary<string,string>[] record)
{
if (record.Length == 0)
{
return "没有任何数据!";
}
else
{
int i = 0;
Dictionary<string,string>[] user = new Dictionary<string,string>[record.Length];
foreach (Dictionary<string,string> stu in record)
{
user[i] = new Dictionary<string,string>();
foreach (string key in stu.Keys)
{
switch (key)
{
case "id":
user[i].Add("id",stu[key]);
break;
case "value":
user[i].Add("value",stu[key]);
break;
default:
break;
}
}
i++;
}

@H_301_0@ //以下代码循环读取拼接返回页面测试,无实际意义
string ss = "";
foreach (Dictionary<string,string> a in user)
{
ss += ";" + a["id"] + "-" + a["value"];
}
return ss;
}

@H_301_0@ }



猜你在找的Ajax相关文章