c# - 为什么在使用数据表复选框时在ajax调用后查询字符串?
问题描述
我正在尝试通过在数据表中选择多个复选框来保存数据。但是在提交中调用ajax后单击ajax未命中成功功能。它与控制器/操作一起显示查询字符串。如下所示
https://localhost:44307/Leaves/Approval?leaveApproveDataTable_length=10&id%5B%5D=11
这是我的js
$(document).on('click', '.btn-Approve', function (e) {
var form = this;
var rows = $(table.rows({
selected: true
}).$('input[type="checkbox"]').map(function () {
return $(this).prop("checked") ? $(this).closest('tr').attr('leaveid') : null;
}));
rows_selected = [];
$.each(rows, function (index, rowId) {
console.log(rowId)
// Create a hidden element
rows_selected.push(rowId);
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
var remarks = $('#Remarks').val();
console.log($(this).closest('tr').attr('leaveid'));
$.ajax({
url: '/Leaves/LeaveApproval',
data: { approveId: rows_selected, remarks: remarks },
type: 'POST',
processData: true,
dataType: 'JSON',
success: function (result) {
console.log(result);
debugger;
if (result) {
window.location.href = "/Leaves/Approval";
}
else {
return result;
}
},
error: function () {
}
});
});
这是我的控制器
public async Task<IActionResult> LeaveApproval(List<int> approveId, string remarks)
{
foreach (int id in approveId)
{
var leave = await _context.Leaves.FindAsync(id);
if (leave == null)
{
return Json(new { success = false });
}
leave.Status = "Approved";
leave.Remarks = remarks;
leave.ApprovedDate = DateTime.Now;
_context.Update(leave);
await _context.SaveChangesAsync();
}
return Json(new { success = true });
}
请帮助我解决问题。
解决方案
从控制器返回时替换: return Json(new { success = true }); 此行带有 return Json(new { success = true }, JsonRequestBehavior.AllowGet);
推荐阅读
- javascript - 我可以使用 orderBychild("person").limitToLast(1) 来获取具有属性“person”的最后一条记录吗?
- flutter - 如何在颤振中使用 dart.core.sink
- raspberry-pi3 - RaspberryPi 在 WLAN 客户端和 AccessPoint 之间交换
- python - 类型提示返回 NameError: name 'datetime' not defined
- sockets - 装配套接字创建崩溃
- android - 三星简易模式下的Android WebView内容布局不同
- bintray - 将库发布到 maven 存储库而不是 JCenter
- javascript - 如何处理烧瓶中的请求json数据
- docker - 将边缘容器从 Google Cloud vision 实施到 Windows 机器
- sql-server - SQL Server 执行速度因向内联表函数提供参数的方式而异