如何通过ajax调用MVC操作方法返回列表< employee>来在jquery数据表中提供数据 [英] How to feed data in jquery datatable by ajax call to MVC action method returning list<employee>
本文介绍了如何通过ajax调用MVC操作方法返回列表< employee>来在jquery数据表中提供数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public JsonResult GetData()
{
Employee emp = new Employee();
列表<员工> emplist = new List< Employee>();
emp.name =Durgesh Mapuskar;
emp.salary =500000;
emp.position =团队领导;
emp.start_date =01/01/2016;
emp.office =BUPA;
emp.extn = 4156;
emplist.Add(emp);
var json = JsonConvert.SerializeObject(emplist);
返回Json(json.ToArray(),JsonRequestBehavior.AllowGet);
}
$(文件).ready(function(){
调试器;
var urllink =/ Home / GetData;
var table = $('#example')。DataTable({
ajax:{
url: urllink,
},
columns:[
{
className:'details-control',
orderable:false,
data:null,
defaultContent:''
},
{data:name},
{data: position},
{data:office},
{data:salary}
],
order:[[1,' asc']]
});
我的尝试:
它给了我未定义的长度属性
解决方案
(document).ready(function(){
debugger;
var urllink =/ Home / GetData;
var table =
('#example')。DataTable({
ajax:{
url:urllink,
},
columns:[
{
className:'details-control',
orderable:false,
data:null ,
defaultContent:''
},
{data:name},
{data:position},
{ data:office},
{data:salary}
],
order:[[1,'asc']]
} );
我的尝试:
它给了我未定义的长度属性
Json
帮助方法 [ ^ ]期望您要返回的对象,而不是已经序列化的JSON字符串。
删除对JsonConvert.SerializeObject
,只需传入列表:
public JsonResult GetData()
{
List< Employee> emplist = new 列表< Employee>();
...
return Json(emplist,JsonRequestBehavior.AllowGet);
}
public JsonResult GetData() { Employee emp = new Employee(); List<Employee> emplist = new List<Employee>(); emp.name = "Durgesh Mapuskar"; emp.salary = "500000"; emp.position = "Team Lead"; emp.start_date = "01/01/2016"; emp.office = "BUPA"; emp.extn = 4156; emplist.Add(emp); var json = JsonConvert.SerializeObject(emplist); return Json(json.ToArray(), JsonRequestBehavior.AllowGet); }
$(document).ready(function () { debugger; var urllink = "/Home/GetData"; var table = $('#example').DataTable({ ajax: { url: urllink, }, "columns": [ { "className": 'details-control', "orderable": false, "data": null, "defaultContent": '' }, { "data": "name" }, { "data": "position" }, { "data": "office" }, { "data": "salary" } ], "order": [[1, 'asc']] });
What I have tried:
it gives me length property not defined
解决方案
(document).ready(function () { debugger; var urllink = "/Home/GetData"; var table =
('#example').DataTable({ ajax: { url: urllink, }, "columns": [ { "className": 'details-control', "orderable": false, "data": null, "defaultContent": '' }, { "data": "name" }, { "data": "position" }, { "data": "office" }, { "data": "salary" } ], "order": [[1, 'asc']] });
What I have tried:
it gives me length property not defined
TheJson
helper method[^] expects the object you want to return, not an already-serialized JSON string.
Remove the call toJsonConvert.SerializeObject
, and just pass in the list:
public JsonResult GetData() { List<Employee> emplist = new List<Employee>(); ... return Json(emplist, JsonRequestBehavior.AllowGet); }
这篇关于如何通过ajax调用MVC操作方法返回列表< employee>来在jquery数据表中提供数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文