发布到控制器的那一刻,屏幕上显示415错误...因此,我认为即使数据通过某种方式我的json结构也不一定正确。
查看Ajax:
function SubAll() {
var selectedValues =
$('#timesheet').DataTable().column(0).checkboxes.selected().toArray();
var instructions = []; //create array of objects
for (var i = 0; i < selectedValues.length; i++) {
instructions.push({ TimeId: selectedValues[i] });
}
var jsonObject = { MasterIds: instructions };
$.ajax({
url: "/Admin/ApproveAllTimesheets",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(jsonObject),
success: function (result) {
console.log(result);
},
error: function (xhr, textStatus) {
if (xhr.status == 401) { alert("Session Expired!"); window.location =
"/Account"; }
else {
alert('Content load failed!', "info");
}
}
});
};
控制器:
public IActionResult ApproveAllTimesheets([FromBody]ValueContainer information)
类对象:
public class ValueContainer
{
public List<Value> MasterIds { get; set; }
}
public class Value
{
public Guid TimeId { get; set; }
}
有效载荷:
{"MasterIds":[{"TimeId":"ad98749f-9083-464b-aac2-0d685a7de809"}]}
更新#1
事实证明,修复方法是我调用该函数的方式。我没有使用按钮onclick =“ SubAll()”来代替它,而是用一个简单的jQuery代替了它来拦截click事件,阻止它,然后调用该函数...现在没有415错误。
查看按钮
<button id="ApproveAll" class="btn btn-success">Approve All</button>
查看jQuery
$(document).ready(function () {
timesheet = $('#timesheet').DataTable({
responsive: {
details: {
renderer: function (api, rowIdx, columns) {
var data = $.map(columns, function (col, i) {
return col.hidden ?
'<tr data-dt-row="' + col.rowIndex + '"
data-dt-column="' + col.columnIndex + '">' +
'<td>' + col.title + ':' + '</td> ' +
'<td>' + col.data + '</td>' +
'</tr>' :
'';
}).join('');
return data ?
$('<table/>').append(data) :
false;
}
}
},
columnDefs: [
{
targets: 0,
orderable: false,
searchable: false,
checkboxes: true
},
{
targets: 5,
visible: false
},
{
targets: 6,
visible: false
}
],
order: [
[1, 'asc']
]
});
$('#ApproveAll').on('click',
function (e) {
var selectedValues =
$('#timesheet').DataTable().column(0).checkboxes.selected().toArray();
var instructions = []; //create array of objects
for (var i = 0; i < selectedValues.length; i++) {
instructions.push(selectedValues[i]);
}
var jsonObject = { MasterIds: instructions };
$.ajax({
url: "/Admin/ApproveAllTimesheets",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(jsonObject),
traditional: true,
statusCode: {
415: function () {
Response.redirect("/Admin/Index");
}
},
success: function (result) {
console.log(result);
},
error: function (xhr, textStatus, errorThrown) {
if (xhr.status == 401) { alert("Session
Expired!"); window.location = "/Account"; }
else {
alert('Content load failed!', "info");
}
}
});
e.preventDefault();
});
});