function delete_fun(id) {
var arr = [];
if (id == undefined) {
$("input:checked").each(function () {
arr.push($(this).val());
});
}
else
arr.push(id);
if (arr.length == 0)
return;
var jsondata = { ids: arr };
$.ajax({
url: "/Manager/Message/Delete?" + Math.random(),
type: "POST",
dataType: "json",
data: JSON.stringify(jsondata),
contentType: 'application/json',
success: function (d) {
if (d.status == 1) {
location.reload();
}
else
alert(d.msg);
}
});
}
JSON.stringify 语法
后台Controllers
[HttpPost]
public JsonResult Delete(long[] ids)
{
try
{
if (ids.Length == 0)
return Json(new { status = 0, msg = "操作失败没有选择操作数据" }, JsonRequestBehavior.AllowGet);
var ms = db.Messages.Where(o => ids.Contains(o.ID));
foreach (var m in ms)
db.Messages.Remove(m);
if (db.SaveChanges() > 0)
return Json(new { status = 1, msg = "" }, JsonRequestBehavior.AllowGet);
else
return Json(new { status = 0, msg = "操作失败" }, JsonRequestBehavior.AllowGet);
}
catch (Exception exc)
{
return Json(new { status = 0, msg = exc.Message }, JsonRequestBehavior.AllowGet);
}
}
做个笔记^_^

本文详细介绍了如何使用JSON.stringify进行数据序列化,并通过AJAX实现后台控制器的POST请求来处理多选删除操作。

被折叠的 条评论
为什么被折叠?



