Way for server side DataTable to write JSON to server in 1.10?

Way for server side DataTable to write JSON to server in 1.10?

eperniceepernice Posts: 4Questions: 0Answers: 0
edited March 2014 in DataTables 1.10
For server side processing, DataTables writes a (much nicer) hierarchy of objects to the server in 1.10, but it breaks ASP.NET MVC ModelBinding, which cannot handle complex hierarchies passed as serialized parameters. Would there be any way to add the ability to pass JSON to the server, rather than params? Or does this ability already exist?

I know that I could use the server side functionality in legacy mode, but I'd prefer not to. I have also asked a question on SO: http://stackoverflow.com/q/22262677/546561

Replies

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    I think jQuery has a method for handling this sort of limitation in ASP.NET already built in using its `traditional` option which is described here: https://api.jquery.com/jQuery.param/

    So you might do:

    [code]
    $('#example').DataTable( function () {
    ajax: {
    url: '/data',
    traditional: true
    }
    } );
    [/code]

    My understanding is that ASP.NET will be able to read that style of parameter... Perhaps you could try it and let me know? If so, I'll put that into the documentation.

    Allan
  • eperniceepernice Posts: 4Questions: 0Answers: 0
    edited March 2014
    Thanks for the response Allan. Unfortunately, `traditional` does not handle hierarchical data structures, so the data would need to be serialized when `traditional` was passed.
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Darn - I missed that part :-(. Perhaps my cunning idea of making the data submitted to the server more "modern" wasn't such a great idea... I think that means that there are basically two options. Use `ajax.data` as a function and convert to a format .NET will work with (perhaps a JSON string in a single parameter?), or switch to the legacy mode...

    Allan
  • eperniceepernice Posts: 4Questions: 0Answers: 0
    Well, I think submitting a modern complex structure is great. The main issue is that it is passing the data as a parameter list (which .NET can only read with `traditional`) rather than as a JSON structure (which .NET has no issue with).

    I haven't tried using `ajax.data` as a function - I'll play with that when I have a chance (hopefully in a few days) and report back.
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    edited March 2014
    Documentation for `ajax.data` , including a couple of examples, is here: http://next.datatables.net/reference/option/ajax.data .

    I'm wondering if it might be an idea for me to offer a plug-in method for the pre-data submit, so a plug-in could be written that would manipulate the data in a particular way - in this case reformatting into something .NET will understand natively. This single plug-in could then be shared in the community saving others the work and reducing code repetition and errors.

    A nice extension might be to have a data retrieval plug-in option as well, so data such as XML, CSV etc could have similar parsers... (Note to self - if this is done, do it in a 1.10 `renderer` to benefit from per table and default options).

    Interested to hear how you get on with `ajax.data` certainly.

    Allan
  • eperniceepernice Posts: 4Questions: 0Answers: 0
    Just wanted to give an update... I didn't end up trying out `ajax.data`, because I started using this MVC ModelBinder that someone wrote, which solves the problem:

    https://github.com/ALMMa/datatables-mvc
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Nice! I didn't know about that project. Added to the DataTables news feed :-). Thanks for pointing it out to us.

    Allan
  • LeoGerberLeoGerber Posts: 1Questions: 0Answers: 0
    edited May 2014

    You can easily achieve the Modelbinding with Datatables for MVC.

    You just have to pass a callback function for the data property in the ajax options. The callback function than returns a json string. .NET can than handle the json and bind the data to your model. You can read more about that here

    Example:

    $('table').dataTable
    ({
                'ajax'    :
                {
                    'url': 'serverSideTableProviderPage',
                    'type': 'POST',
                    'contentType': 'application/json; charset=utf-8',
                    'data':function(data)
                     {
                           return data = JSON.stringify(data);
                     }
                },
               [...more options...]
            }
    });
    
This discussion has been closed.