我的ajax调用返回后,我目前在浏览器中收到以下错误:
Cannot read property 'length' of undefined
这是我的HTML:
<table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="dyntable">
<colgroup>
<col class="con0" />
<col class="con1" />
<col class="con0" />
<col class="con1" />
</colgroup>
<thead>
<tr>
<th>Id</th>
<th>SerialNumber</th>
<th>AccountCode</th>
<th>ClientId</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
这是JS:
<script type="text/javascript">
$(document).ready(function () {
$('#dyntable').dataTable({
stateSave: true,
serverSide: true,
searchDelay: 300,
autoWidth: false,
ajax: {
url: 'Search',
type: 'POST',
},
order: [[0, "asc"]],
processing: true,
columns: [
{
"sName": "Id",
"bSortable": false
},
{
"sName": "SerialNumber",
"bSortable": true
},
{
"sName": "AccountCode",
"bSortable": true
},
{
"sName": "ClientId",
"bSortable": true
}
]
});
// DataTables Length to Select2
jQuery('div.dataTables_length select').removeClass('form-control input-sm');
jQuery('div.dataTables_length select').css({ width: '60px' });
jQuery('div.dataTables_length select').select2({ minimumResultsForSearch: -1 });
});
</script>
最后是我的行动方法:
[HttpPost]
public async Task<ActionResult> Search(FilterRequest filter)
{
try
{
IQueryable<SerialRouting> filteredItems = db.SerialRoutings;
if (!string.IsNullOrEmpty(filter.Search.Value))
{
filteredItems = db.SerialRoutings.Where(c =>
c.AccountCode.Contains(filter.Search.Value)
|| c.Client.ClientID.Contains(filter.Search.Value)
|| c.Serialnumber.ToString().Contains(filter.Search.Value)
);
}
int totalRecords = filteredItems.Count();
filteredItems = filteredItems.OrderBy(x => x.Serialnumber);
filteredItems = filteredItems.Skip(filter.Start).Take(filter.Length);
var displayItems = await filteredItems.Select(x => new SerialRoutingViewModel { Id = x.SerialRoutingIdentity, SerialNumber = x.Serialnumber, AccountCode = x.AccountCode, ClientId = x.Client.ClientID }).ToListAsync();
return Json(new PageResponse<SerialRoutingViewModel>(filter.Draw, totalRecords, displayItems.Count, displayItems.ToArray()));
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return null;
}
对于ModelBindings来自以下库:MVC.DataTables
我正在使用数据表v 1.10.10
我错过了什么?