set Page length from server side MVC

set Page length from server side MVC

noobplusnoobplus Posts: 2Questions: 1Answers: 0
edited July 2019 in Free community support

Hi,
I am creating a datatable from server side MVC Controller. But all datas are displayed in single page. Paging is not working
This is my CSHTML

var tbl = $('#' + table).DataTable({
                    "serverSide": true,
                    "stateSave": true,
                    "processing": true,
                    "paging": true,
                    "columns": [
                        { "data": "number", "orderable": true, "name": "number" },
                        { "data": "number", "orderable": true, "name": "number1" }
                    ],
                    "ajax": {
                        "url": "/PayModule/datasource",
                        "type": "POST",
                        "datatype": "json"
                    },
                });

This is my MVC Controller

    [HttpPost]
    public ActionResult datasource()
    {
        string i = Request["start"];
        DataTable dt = DataHelper.GetTable("select top 1000 number from infinity");
        List<Employee> lst = new List<Employee>();
        lst = (from DataRow row in dt.Rows

               select new Employee
               {
                   number = row["number"].ToString(),

               }).ToList();
        return Json(new {
            aaData = lst,
            //order= "asc",
            //start=20,
            //iDisplayStart=20,
            //length=10,
            //Data = lst,
            sEcho = 1,//draw=1,
            aaLength=10,
            iTotalRecords = 50,
            iTotalDisplayRecords = 10,
            //iDisplayLength=10,
            //bDeferRender= true,
            //deferLoading=50
        },JsonRequestBehavior.AllowGet);
    }
    class Employee
    {
        public string number { get; set; }
    }

I sent aaData and it is working successfully. But all datas are displayed in a single page. I tried iDisplayLength=10,length=10 in Json from MVC Controller but in vain. Thanks in advance

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin
    edited July 2019 Answer ✓

    Sounds like you aren't limiting the amount of data that the server is sending back to the client per the requested parameters. Indeed, in the above, I see you are using select top 1000 which is presumably getting up to 1000 rows - not just the 10 that DataTables is looking for.

    If you only have a 1000 records, don't bother with server-side processing. Only when you get to tens of thousands do you need to start thinking about it, and only by 100k (depending on the record size) will it become necessary.

    Allan

  • noobplusnoobplus Posts: 2Questions: 1Answers: 0
    edited July 2019

    Hi Allan @allan
    Thanks for your kind reply.
    Extremely Sorry for adding top 1000. I added it while testing, but I am about to show all the lakhs of records. And I get 1 more doubt in this. Is it possible to keep checkbox checked state on one page maintain its state when navigating to other pages?
    Thanks in advance

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin
    Answer ✓

    Is it possible to keep checkbox checked state on one page maintain its state when navigating to other pages?

    If you are using server-side processing then its a bit difficult. You'd need to maintain a list of which checkboxes have been checked and then recheck them on each page draw. DataTables doesn't do that out of the box.

    It would work with client-side processing automatically though.

    Allan

This discussion has been closed.