我一直在苦苦挣扎,无处不在,但无法找到解决方案。我想在$ .ajax中传递数组数据,但不知道如何。以下是代码。
$("#procressGrid").click(function () {
var dataArray = [];
$.each($(".gridTable tr:not(.gridTitleRow)"), function(a, b){
var id = $("input.idField", b).val();
var code = $("input[name='code']", b).val();
dataArray.push({
"id": id,
"code": code
})
});
$.ajax({
url: "/HeaderMenu/So",
type: "POST",
data: { backerEntries[]: dataArray }
dataType: "json",
contentType: "application/json; charset=utf-8",
beforeSend: function () { $("#statusDropdown").fadeIn(); },
complete: function () { $("#statusDropdown").fadeOut(); },
success: function (data) {
if (data.Status == "Success") {
} else {
}
},
error: function () {
$("#error").show().html("An error has occured!!!");
}
});
});
和在MVC3控制器中声明什么?
public ActionResult So(Array backerEntries)
{
//var b = a;
return Json(new { status = "Success", message = "Passed" });
}
答案 0 :(得分:4)
我会更改您的$.ajax
电话:
$.ajax({
/*snip */
data: dataArray
});
在服务器端创建一个绑定到的视图模型:
public class BackerEntry
{
public string Id { get; set; }
public string Code { get; set; }
}
现在你的行动将采用这些类型的数组:
public ActionResult So(BackerEntry[] backerEntries)
{
// ...
}
答案 1 :(得分:0)
这对你有用吗?
var dataArray = new Array();
// fill your array
$.ajax({
url: "/HeaderMenu/So",
type: "POST",
data: {'backerEntries' : dataArray},
// the rest of your code
});
答案 2 :(得分:0)
答案 3 :(得分:0)
var fields = $("
.gridTable tr:not(.gridTitleRow) input.idField,
.gridTable tr:not(.gridTitleRow) input[name='code']
").serializeArray();
$。AJAX({ ...
data: { backerEntries: fields }
... });
可以试试这个序列化数组吗?