Jquery Datatable Server Side processing in ASP.NET MVC application

Jquery Datatable Server Side processing in ASP.NET MVC application

pras9224pras9224 Posts: 4Questions: 1Answers: 0

Hi,

I am using Jquery Datatable Server side processing in an ASP.NET MVC application. The table gets rendered properly but the controller method which is defined as server side processing method gets called repeatedly (4 to 5 times). Also, when I redirect to some other page, this method gets called.
Below are the properties set for server side processing

oTable = $('#activitiesTable').dataTable({
"bStateSave": true,
"sPaginationType": "full_numbers",
"aaSorting": [[5, "asc"]],
"bServerSide": true,
"sAjaxSource": "FilterActivitiesData",
"bProcessing": false,
"sServerMethod": "POST"
})

Please help me to understand what can be the reason behind multiple calls to server side method.

Thanks in advance.

Answers

  • AshbjornAshbjorn Posts: 55Questions: 2Answers: 16
    edited July 2015

    Hi pras9224,

    I am not sure why your behavior is happening, but maybe it can help if I show you how I create my DataTables using MVC:

    $(document).ready(function () {
      var table = $('#list-of-model').DataTable({
          "ajax": {
              "url": "@Url.Action("list", "devices")",
              "type": "POST"
          },
          "deferRender": true,
          "serverSide": true,
          "searchDelay": 800,
          "autoWidth": true,
          "stateSave": true
      });
    });
    

    Hope this helps,

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    edited July 2015

    Please help me to understand what can be the reason behind multiple calls to server side method.

    I don't see anything wrong with your code. Could you link to the page so we can take a look and debug it please?

    Allan

  • pras9224pras9224 Posts: 4Questions: 1Answers: 0

    Hi allan, Ashbjorn

    Thanks for the response.

    Please see below link, my problem is due to same reason mentioned in it.
    http://stackoverflow.com/questions/15478081/datatables-generating-multiple-server-side-requests

    In my case, I am also using fnSetColumnVis() function to hide some columns based on User Role.
    I am hiding 4 columns, so it is calling server method 4 times additionally.

    Few more observations:
    When I am not hiding any column, in that case server side method is getting called 2 times as I have one default sort column.
    When I have some search string in search box, in that case server side method is getting called 3 times.
    So when I hide 4 columns now, server side method was getting called 6 or 7 times.

    What I think is, it is calling server side method first time for rendering datatable, 2nd time for sorting, 3rd time if search string is present and then for each column which is hidden using fnSetColumnVis().

    Can you please suggest any better approach to avoid this? Or let me know if my above analysis is wrong somewhere.

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    If you use the columns().visible() method to hide columns it will not perform a redraw (i.e. an Ajax request).

    Allan

  • pras9224pras9224 Posts: 4Questions: 1Answers: 0

    Thanks allan.
    using columns().visible() has solved the issue.
    But columns().visible() is an DataTable api and in order to use it I need to initialize datatable as $( '#activitiesTable').DataTable() this as it returns DataTables API.
    and $( '#activitiesTable' ).dataTable() this returns jQuery object.
    $( '#activitiesTable').DataTable() by using this I am not able to use Datatable functions like fnDisplayStart().
    Can you please suggest an approach so that I can use both DataTable functions as well as DataTable API?

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    The best thing to do would be to upgrade to the new API fully. But if you want to access both the legacy and "new" API, then the API reference page shows how:

    var jqTable = $('#myTable').dataTable();
    var dt = jqTable.api();
    

    Allan

  • pras9224pras9224 Posts: 4Questions: 1Answers: 0

    Thanks allan. Its working for me now.

This discussion has been closed.