locked
Datatables Jquery "Server side" - No matching records found RRS feed

  • Question

  • User490317677 posted

    I have Jquery Datatables server side and When i navigate to my page to see results in Datatables it displays "No matching records found" and than i debugged my code i can see its retrieve currect data and i also check Response in browser it is also retrieve data.

    Json Response:

    {"data":
    [{"Kundenavn":"test 1"},
    {"Kundenavn":"test 2"},
    {"Kundenavn":"test 3"}]
    ,"draw":"1","recordsTotal":567,"recordsFiltered":567}

    JavaScript:

    <table id="OrdrerList">
        <thead>
            <tr>
                <th>Kundenavn</th>
            </tr>
    
        </thead>
        <tbody>
        </tbody>
    </table>
    
    $('#OrdrerList').DataTable({
    
     "processing": true,
     "serverSide": true,
    
        ajax:{
            url: '@Url.Action("GetClosedRMA", "User")',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            dataSrc: ''
    
            },
        columns: [
    
            { data: "Kundenavn" }
       ]
    });

    Controller:

    public JsonResult GetClosedRMA()
    {
        //Server side Parameters
    
        int start = Convert.ToInt32(Request["start"]);
        int length = Convert.ToInt32(Request["length"]);
        string searchValue = Request["search[value]"];
        string sortColumnName = Request["columns["+ Request["order[0][colmun]"]+ "][name]"];
        string sortDirection = Request["order[0][dir]"];
    
        List<RMAclosedCase> rmalist = new List<RMAclosedCase>();
        using (Namespace db = new Namespace())
        {
           rmalist = (from RH in db.RMA_History
                       join RS in db.RMAStatus on RH.StatusID equals RS.ID
                       where RH.SagesType == "LUK"
                       select new RMAclosedCase
                       {
                           Kundenavn = RH.Kundenavn
    
                       }).ToList();
    
           int totalrows = rmalist.Count();
                //Filter
                if (!string.IsNullOrEmpty(searchValue))
                {
                    rmalist = rmalist.Where(x => x.Kundenavn.ToLower().Contains(searchValue.ToLower())).ToList();
                }
           int totalrowsefterfiltering = rmalist.Count();
    
            //Paging
            rmalist = rmalist.Skip(start).Take(length).ToList();
    
            return Json(new {data= rmalist,draw = Request["draw"], recordsTotal = totalrows,recordsFiltered = totalrowsefterfiltering },JsonRequestBehavior.AllowGet);
        }
    }

    What did i wrong?! :) Can anyone please help me or point me into right direction! :) Thanks in advance

    Friday, January 11, 2019 9:30 AM

Answers

  • User1520731567 posted

    Hi ManDown,

    Accordind to your code,I suggest you could pay attention the response of the json format,I modify and you could refer to it:

    Controller:

           public class DataTable2
            {
                public int draw { get; set; }
                public int recordsTotal { get; set; }
                public int recordsFiltered { get; set; }
                public RMAclosedCase[] data { get; set; }
            }
    
            public class RMAclosedCase
            {
                public string Kundenavn { get; set; }
            }
            public ActionResult Studentsdatatable()
            {
                DataTable2 dataTable = new DataTable2();
    
                List<RMAclosedCase> rmalist = new List<RMAclosedCase>();
                rmalist.Add(new RMAclosedCase { Kundenavn = "8B" });
                rmalist.Add(new RMAclosedCase { Kundenavn = "7A" });
    
        
                dataTable.data = rmalist.ToArray();
                dataTable.recordsTotal = rmalist.Count;
                dataTable.recordsFiltered = rmalist.Count();
                return Json(dataTable, JsonRequestBehavior.AllowGet);
    return Json(new {data= rmalist,draw = Request["draw"], recordsTotal = totalrows,recordsFiltered = totalrowsefterfiltering },JsonRequestBehavior.AllowGet);
    }

    Javascript:

     var filterValues = {};
            $(document).ready(function () {
                var refDataTable = $("#OrdrerList").dataTable({
                    serverSide: true,
                    bFilter: false,
                    columns: [
                        { data: "Kundenavn" }
                    ],
                    ajax: function (data, callback, settings) {
                        $.ajax({
                            url: '/Home/Studentsdatatable',
                            method: 'GET'
                        }).done(callback);
                    }
                });
            });

    I can retrieve data in browser,like the picture:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 14, 2019 8:11 AM
  • User490317677 posted

    i solved the issue with add this line in Ajax data: 'dataSet'

    and than i got all data displayed in datatables :) and i also tried your solution its also working fine ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 7:48 AM

All replies

  • User1520731567 posted

    Hi ManDown,

    Accordind to your code,I suggest you could pay attention the response of the json format,I modify and you could refer to it:

    Controller:

           public class DataTable2
            {
                public int draw { get; set; }
                public int recordsTotal { get; set; }
                public int recordsFiltered { get; set; }
                public RMAclosedCase[] data { get; set; }
            }
    
            public class RMAclosedCase
            {
                public string Kundenavn { get; set; }
            }
            public ActionResult Studentsdatatable()
            {
                DataTable2 dataTable = new DataTable2();
    
                List<RMAclosedCase> rmalist = new List<RMAclosedCase>();
                rmalist.Add(new RMAclosedCase { Kundenavn = "8B" });
                rmalist.Add(new RMAclosedCase { Kundenavn = "7A" });
    
        
                dataTable.data = rmalist.ToArray();
                dataTable.recordsTotal = rmalist.Count;
                dataTable.recordsFiltered = rmalist.Count();
                return Json(dataTable, JsonRequestBehavior.AllowGet);
    return Json(new {data= rmalist,draw = Request["draw"], recordsTotal = totalrows,recordsFiltered = totalrowsefterfiltering },JsonRequestBehavior.AllowGet);
    }

    Javascript:

     var filterValues = {};
            $(document).ready(function () {
                var refDataTable = $("#OrdrerList").dataTable({
                    serverSide: true,
                    bFilter: false,
                    columns: [
                        { data: "Kundenavn" }
                    ],
                    ajax: function (data, callback, settings) {
                        $.ajax({
                            url: '/Home/Studentsdatatable',
                            method: 'GET'
                        }).done(callback);
                    }
                });
            });

    I can retrieve data in browser,like the picture:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 14, 2019 8:11 AM
  • User490317677 posted

    Thanks , Yuki Tao i will try :)

    Tuesday, January 15, 2019 10:44 AM
  • User490317677 posted

    i solved the issue with add this line in Ajax data: 'dataSet'

    and than i got all data displayed in datatables :) and i also tried your solution its also working fine ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 7:48 AM