如何通过ajax调用MVC操作方法返回列表< employee>来在jquery数据表中提供数据 [英] How to feed data in jquery datatable by ajax call to MVC action method returning list<employee>

查看:111
本文介绍了如何通过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


The Json helper method[^] expects the object you want to return, not an already-serialized JSON string.

Remove the call to JsonConvert.SerializeObject, and just pass in the list:

public JsonResult GetData()
{
    List<Employee> emplist = new List<Employee>();
    
    ...
    
    return Json(emplist, JsonRequestBehavior.AllowGet);
}


这篇关于如何通过ajax调用MVC操作方法返回列表&lt; employee&gt;来在jquery数据表中提供数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆