Load ASP.Net MVC JSONResult jQuery DataTables

后端 未结 2 2018
暗喜
暗喜 2020-12-08 03:35

I\'m trying to get the DataTables(http://datatables.net) to work with a JsonResult returned by an ASP.Net MVC Controller. I keep getting a \"DataTables warning (table id = \

2条回答
  •  时光说笑
    2020-12-08 03:52

    The following works great for me:

    $(function () {
        $('#example').dataTable({
            bProcessing: true,
            sAjaxSource: '@Url.Action("LoadPhoneNumbers", "Home")'
        });
    });
    

    I have removed the sAjaxDataProp property.

    with this data source:

    public ActionResult LoadPhoneNumbers()
    {
        return Json(new
        {
            aaData = new[] 
            {
                new [] { "Trident", "Internet Explorer 4.0", "Win 95+", "4", "X" },
                new [] { "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", "1.8", "A" },
                new [] { "Webkit", "iPod Touch / iPhone", "iPod", "420.1", "A" }
            }
        }, JsonRequestBehavior.AllowGet);
    }
    

    and for your example with phones simply:

    public ActionResult LoadPhoneNumbers()
    {
        var phoneNumbers = new List(new[] 
        {
            new PhoneNumber { Number = "555 123 4567", Description = "George" },
            new PhoneNumber { Number = "555 765 4321", Description = "Kevin" },
            new PhoneNumber { Number = "555 555 4781", Description = "Sam" }
        });
    
        return Json(new
        {
            aaData = phoneNumbers.Select(x => new[] { x.Number, x.Description })
        }, JsonRequestBehavior.AllowGet);
    }
    

提交回复
热议问题