Jquery数据表显示消息未找到匹配的记录

时间:2015-12-29 09:30:56

标签: jquery json asp.net-mvc datatable datatables

我想将收到的json数据绑定到jquery数据表。我有以下问题如下。 1.我的HTML代码是否正确? 2.收到的Json数据格式是否正确? 3.我使用正确的脚本来渲染jquery数据表请帮助。

我附上代码以及收到的数据的屏幕截图。

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 });
            //System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            //string aaData = jSearializer.Serialize(l);
            //Console.WriteLine(FirstNameCount + "" + LastNameCount + "" + TotalSalary + "" + GenderCount);
            // aaData = "{\"data\": " + aaData + "}";
            var todoListsResults = l.Select(
                 a => new
                 {

                     a.FirstNamecount,
                     a.LastNamecount,
                     a.Salary,
                     a.Gendercount

                 });

            var jsonData = new
            {
               iTotalRecords= "54",
               iTotalDisplayRecords="22",
               aadata = todoListsResults
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);


        }


Jquery script:-
function callme(fncount, lncount, i, gndr) { 


        $('#example').dataTable({
            "ajax": {
                url: "/TodoList/This_is_final",
                type: "POST",
                data: {
                    'FirstNameCount': fncount, 'LastNameCount': lncount, 'TotalSalary': i, 'GenderCount': gndr
                },
                dataSrc: function (json) {

                    alert(JSON.stringify(json));
                    return { "data":json };
                }
            },
            "processing": true,
            "serverSide": true,
            "ordering": false,
            "searching": true,
            "columns": [
                 { "mdata": "FirstNamecount" },
                 { "mdata": "LastNamecount" },
                 { "mdata": "TotalSalary" },
                 { "mdata": "GenderCount" }
            ]
        });

}

Image

1 个答案:

答案 0 :(得分:0)

我也使用datatable进行数据绑定,但我使用JQuery post函数将JsonResult数据作为

$.post("../TodoList/This_is_final", { "FirstNameCount": fncount, "LastNameCount": lncount},
    function (data) { if (data != null) {
           var table = $("#example").dataTable({
                        "iDisplayLength": 10,
                        "aaData": data,
                        "aoColumns": [
                                  { "sTitle": "First Name", "sWidth": "20%"},
                                  { "sTitle": "Last Name", "sWidth": "20%"}]
})} }

在视图中

public JsonResult This_is_final(int FirstNameCount, int LastNameCount)
    {
        List<Counting> l = new List<Counting>();
        l.Add(new Counting() { FirstNamecount = FirstNameCount, LastNamecount = LastNameCount });
          var todoListsResults = l.Select(
             a => new[]
             {

                 a.FirstNamecount,
                 a.LastNamecount,                     
             }).ToArray();

       return Json(todoListsResults, JsonRequestBehavior.AllowGet);
    }

更新了您的代码..