我有一个代码,代码的主要目的是实现jquery数据表并呈现json数据。我成功地从控制器以json格式获取数据。当我运行代码时,它显示一个警告json数据从服务器无法解析。请帮忙。我也附上了代码。
public JsonResult This_is_final(int FirstNameCount, int LastNameCount, int TotalSalary, int GenderCount)
{
List<Counting> l = new List<Counting>();
l.Add(new Counting() { FirstNamecount = FirstNameCount, LastNamecount = LastNameCount, Salary = TotalSalary, Gendercount = GenderCount });
var todoListsResults = l.Select(
a => new
{
a.FirstNamecount,
a.LastNamecount,
a.Salary,
a.Gendercount
});
var jsonData = new
{
aadata= todoListsResults
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
function callme(fncount, lncount, i, gndr) {
var dataset = [fncount, lncount, i, gndr];
//var oTable = $('#example').dataTable();
$.post('/TodoList/This_is_final', { 'FirstNameCount': fncount, 'LastNameCount': lncount, 'TotalSalary': i, 'GenderCount': gndr }, function (data) {
alert(JSON.stringify(data));
$('#example').dataTable({
"aaData": data,
"bProcessing": true,
"bServerSide": true,
"bDestroy": true,
"bRetrieve": true,
"aoColumns": [
{ "aaData": "FirstNamecount" },
{ "aaData": "LastNamecount" },
{ "aaData": "TotalSalary" },
{ "aaData": "GenderCount" }
]
});
});
}
答案 0 :(得分:0)
使用适合您的数据格式的正确DataTables初始化代码如下所示:
$('#example').dataTable({
"aaData": data,
"bDestroy": true,
"sAjaxDataProp": "aadata",
"aoColumns": [
{ "mData": "FirstNamecount" },
{ "mData": "LastNamecount" },
{ "mData": "Salary" },
{ "mData": "Gendercount" }
]
});
答案 1 :(得分:-1)
将数据传递给aaData
时,您需要对数据进行字符串化。
"aaData": JSON.stringify(data),