我正在使用带有asp.net MVC5的JQuery Datatable,但每次我收到错误时都说'#34; DataTables警告:table id = {id} - 请求的未知参数' {parameter}'对于行{row-index},列{column-index}`"
以下是代码详情
查看详情如下
<p>
<div>
Total employees are @ViewBag.records
<p>
<button id="btnCall" value="Call">Call</button>
</p>
<table id="employeeDataTable">
<thead>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
<th>Designation</th>
<th>DepartmentName</th>
<th>Contact</th>
<th>EmailAddress</th>
<th>Location</th>
</tr>
</thead>
</table>
</div>
</p>
@section Scripts{
<script type="text/javascript">
$(document).ready(function ()
{ $("#employeeDataTable").dataTable({
serverSide: true,
ajax: {
url: '@Url.Action("DataTableUse", "DataTable")',
type: "GET",
datatype: "json"
},
"columns": [
{ "data": "ID","autoWidth": true },
{ "data": "FirstName", "autoWidth": true },
{ "data": "LastName", "autoWidth": true },
{ "data": "Designation", "autoWidth": true },
{ "data": "DepartmentName", "autoWidth": true },
{ "data": "Contact", "autoWidth": true },
{ "data": "EmailAddress", "autoWidth": true },
{ "data": "Location", "autoWidth": true },
]
});
});
</script>
}
使用的脚本是
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="/Scripts/jquery-1.10.2.js"></script>
控制器代码
public ActionResult Index()
{
return View("DataTableUse");
}
public ActionResult DataTableUse()
{
ClsDataOperation objDO = new ClsDataOperation();
List<Employee> lstEmployees = new List<Employee>();
lstEmployees = objDO.GetEmployeeData();
ViewBag.records = lstEmployees.Count;
string strJsonResultSet = JsonConvert.SerializeObject(lstEmployees) ;
return Json(new { data = strJsonResultSet },JsonRequestBehavior.AllowGet);
}
模型类详细信息
public class Employee
{
public int ID { get; set; }
public string FirstName{ get; set; }
public string LastName{ get; set; }
public string Designation { get; set; }
public string DepartmentName { get; set; }
public string Contact { get; set; }
public string EmailAddress { get; set; }
public string Location { get; set; }
}
相同的结构在数据库表中,数据如下JSON格式
[{"ID":1,"FirstName":"FooTest1","LastName":"Cool1","Designation":"Director","DepartmentName":"IT","Contact":"123456789","EmailAddress":"FooTest1.Cool1@test.com","Location":"NJ"},
{"ID":2,"FirstName":"FooTest2","LastName":"Cool2","Designation":"Director","DepartmentName":"IT","Contact":"123456789","EmailAddress":"FooTest2.Cool2@test.com","Location":"NJ"}]
DataOpeartion类的代码如下
public List<Employee> GetEmployeeData()
{
List<Employee> lstEmployees = new List<Employee>();
try
{
using (var db = new DevelopmentTestDatabaseContext())
{
//lstEmployees = db.EmployeeData.ToList<Employee>();
lstEmployees = db.EmployeeData.ToList();
}
}
catch (Exception ex)
{
}
return lstEmployees;
}
DataContext Class如下
public class DevelopmentTestDatabaseContext :DbContext
{
public DevelopmentTestDatabaseContext() : base("name =DevelopmentTestDatabaseContext")
{
}
public virtual DbSet<Employee> EmployeeData { get; set; }
}
每次我收到的错误都是&#34; DataTables警告:请求的未知参数&#39; 0&#39;来自行&#39; 0&#39;的数据源&#34;
请帮我解决这个问题。
答案 0 :(得分:0)
基于this link,我认为您可能需要将dataSrc属性作为空字符串添加到ajax中。这是因为您返回的json不是具有data属性的对象,而是数组。
ajax: {
url: '@Url.Action("DataTableUse", "DataTable")',
type: "GET",
datatype: "json",
dataSrc: ""
},
答案 1 :(得分:0)
我能够解决这个问题,在下面的行中,我在使用JSON返回时使用字符串,因此删除了
#include "foo.h"
而不是我直接使用lstEmployees获取数据,如下所示
string strJsonResultSet = JsonConvert.SerializeObject(lstEmployees) ;
return Json(new { data = strJsonResultSet },JsonRequestBehavior.AllowGet);
然后返回时在return语句中添加了以下参数
return Json(new { data = lstEmployees },JsonRequestBehavior.AllowGet)
感谢Mark和Grant的帮助。