首页 > 解决方案 > 为什么在使用数据表复选框时在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 }); 
        }

请帮助我解决问题。

标签: c#jqueryasp.netdatatable

解决方案


从控制器返回时替换: return Json(new { success = true }); 此行带有 return Json(new { success = true }, JsonRequestBehavior.AllowGet);


推荐阅读