DataTables 警告:表 id=users - Ajax 错误。使用 jquery 将参数传递给它时
Posted
技术标签:
【中文标题】DataTables 警告:表 id=users - Ajax 错误。使用 jquery 将参数传递给它时【英文标题】:DataTables warning: table id=users - Ajax error. when passing parameter to it using jquery 【发布时间】:2021-04-27 14:31:24 【问题描述】:将参数传递给数据表 jQuery 时出现此错误。
如果我将控制器更改为此
public ActionResult GetUsers(int divisionId)
没有参数它工作正常,但我需要传递参数。
DataTables 警告:表 id=users - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7 这是我的看法
<div>
<div class="row row-xs align-items-center mg-b-20">
<div class="col-md-4">
<label class="form-label mg-b-0">Division</label>
</div>
<div class="col-md-8 mg-t-5 mg-md-t-0">
<select id="selection" class="form-control select-group">
<option value="0">-- Select User --</option>
@foreach (var user in Model.divisions)
<option value="@user.DivisionID">@(user.DivisionName)</option>
</select>
</div>
</div>
<div class="table-responsive">
<table id="users" class="table table-striped text-md-nowrap">
<thead>
<tr>
<th>User ID</th>
<th>Employee ID</th>
<th>Username</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
</table>
</div>
</div>
控制器
public ActionResult GetUsers(int divisionId)
int pageSize = 0, skip = 0;
string orderBy = "", draw = "", search = "";
Utility.SetDataTableValues(Request, out draw, out orderBy, out search, out pageSize, out skip);
int recordsTotal = 0;
List<UserPartial> usersList = new List<UserPartial>();
usersList = UserBL.GetUsersByDivision(pageSize, skip, out recordsTotal,2, search);
return Json(new draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = usersList , JsonRequestBehavior.AllowGet);
和脚本:
$(function ()
$("#selection").on('change', function ()
var table = $('#users').DataTable();
table.destroy();
$("#users").show();
var divisionId = $("#selection option:selected").val();
var url = '/AssignHod/GetUsers/';
table = $('#users').DataTable(
"processing": true,
"serverSide": true,
"filter": true,
"orderMulti": false,
"destroy": true,
"ordering": true,
"ajax":
"url": '@Url.Action("GetUsers", "AssignHod")',
"type": "POST",
"datatype": "json"
,
"columns":
[
"data": "UserID"
,
"data": "EmployeeId"
,
"data": "Username"
,
"data": "Email"
,
"render": function (data, type, row, meta)
var html = "";
//if (row.canEdit)
if (row.isHOD)
html += '<a title="Mark as User" href="/AssignHoD/deactivate/' + row.UserID + '" class="btn btn-sm waves-effect waves-light btn-outline-info"><i class="fa fa-toggle-off"></i></a>';
else if (!row.isHOD)
html += '<a title="Mark as H.O.D" href="/AssignHoD/activate/' + row.UserID + '" class="btn btn-sm waves-effect waves-light btn-outline-info"><i class="fa fa-toggle-on"></i></a>';
return html;
]
,
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull)
if (aData[0])
);
);
);
【问题讨论】:
【参考方案1】:我在ActionResult
之前添加了属性路由,它现在可以工作了。
[Route("AssignHoD/GetUsers/Id")]
public ActionResult GetUsers(int Id)
【讨论】:
以上是关于DataTables 警告:表 id=users - Ajax 错误。使用 jquery 将参数传递给它时的主要内容,如果未能解决你的问题,请参考以下文章
DataTables 警告(表 id = 'table-filter'):从数据源请求未知参数 '0' 用于数据表中的第 0 行错误
DataTables 警告:table id=dataTables - Ajax 错误。 404 未找到
DataTables 警告:table id=DataTables_Table_1 - 无法重新初始化 DataTable。不工作
DataTables 警告:table id=example - 无法重新初始化 DataTable - jQuery
ajax datatable - DataTables警告:table id = example - 无法重新初始化DataTable