Ajax请求没有将更改的字符串传递给JsonResult参数?

时间:2020-10-16 20:58:14

标签: json ajax asp.net-core

每次尝试更改值时,我都试图通过Ajax调用将两个元素的值传递给我的JsonResult参数,但它没有选择新值(保持为旧值)。

我的html和javasript:

<select class="form-control" id="selspecialist" asp-for="SelSpecialist" asp-items="Model.SpecialistList"></select>
<select class="form-control" id="selreqtype" asp-for="SelReqType" asp-items="Model.ReqTypeList"></select>
    $(document).ready(function () {
        $('#mytable').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: "?handler=loadListJson" + "&selSpecialist=" + document.getElementById("selspecialist").value
                        + "&selReqType=" + document.getElementById("selreqtype").value,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("RequestVerificationToken",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
            },
            columns: [
                { data: "part" },
                { data: "requestTo" },
                { data: "subGroup" },
                { data: "dueDt", type: 'date' },
                { data: "desc" },
                { data: "reason" },
                { data: "reqType" },
                { data: "lastUpdateDt", visible: false },
                {
                    data: null,
                    render: function (data, type, row) {
                        return "<a href='#' class='btn btn-primary btn-xs' onclick=LoadPartUpdate('" + row.part + "'); ><i class='fa fa-edit' aria-hidden='true' style='color:#ffffff; font-size:13px;'></i></a>";
                    }
                }
            ]
        });
        $('#selspecialist').on('change', function () { $('#mytable').DataTable().draw(); });
        $('#selreqtype').on('change', function () { $('#mytable').DataTable().draw(); });
    });

我的JsonResult方法:

public JsonResult OnPostLoadListJson(string selSpecialist, string selReqType)
        {
            vwOpenRequests = _context.vwOpenRequests.OrderBy(x => x.Sort);
            if (selSpecialist != null) { vwOpenRequests = vwOpenRequests.Where(s => s.RequestTo.Contains(selSpecialist)); }
            if (selReqType != null) { vwOpenRequests = vwOpenRequests.Where(s => s.ReqType.Contains(selReqType)); }
            return new JsonResult(new { data = vwOpenRequests });
        }

1 个答案:

答案 0 :(得分:1)

修改onchange事件,您应该使用以下新参数重新加载数据表:

$('#selspecialist').on('change', function () {
    $('#mytable').DataTable().ajax.url("?handler=loadListJson" + "&selSpecialist=" + document.getElementById("selspecialist").value
        + "&selReqType=" + document.getElementById("selreqtype").value).load();
});
$('#selreqtype').on('change', function () {
    $('#mytable').DataTable().ajax.url("?handler=loadListJson" + "&selSpecialist=" + document.getElementById("selspecialist").value
        + "&selReqType=" + document.getElementById("selreqtype").value).load();
});