How to access the ajax.data object for server-side processing?

How to access the ajax.data object for server-side processing?

steven408steven408 Posts: 9Questions: 3Answers: 1
edited October 2019 in Free community support

I've seen https://datatables.net/reference/option/ajax.data and other examples on how to send custom HTTP variables to the server. But I'm having trouble understanding how to send the object as an parameter. I want to be able to view all the parameters the DataTable sends to the server as an object rather than individual parameters.

My setup is as follows:

$(document).ready(function () {
            $('#example').DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    "url": '/Browse/GetRecordsAsync',
                    "type": 'POST',
                    "datatype": "json",
                    "data": {
                        "sentData": $.ajax.data,
                        "search": "asd"
                    }
                },
                columns: [
                    { "data": "name"},
                    { "data": "occupation"},
                    { "data": "salary"},
                ],
            });
        });

In my controller, the signature of my controller action is:
public async Task<ActionResult> GetRecordsAsync(object sentData, int draw, int start, int length, string search)

This was a simple experiment to see what data is being sent to the server. I am not sure how to get the returned object (documentation states ajax.data option sends this info) and how to set up the method signature to properly access that object. Also, I am not sure how to setup the method parameters to access search[value] or order[i][column] from https://datatables.net/manual/server-side#Returned-data

I'm reading the documentation and I still don't understand how to access the returned object or the specific parameters like search[value] from my controller.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

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

    You could try:

    ajax: {
      ...,
      data: function (d) {
        return {
          sentData: JSON.stringify(d),
          draw: d.draw,
          length: d.length,
          ...
      }
    }
    

    Is that how C#'s parameter name mapping works for controllers? You sent an HTTP parameter with the name you want for the parameter?

    Allan

  • steven408steven408 Posts: 9Questions: 3Answers: 1
    Answer ✓

    It seems that DataTables automatically sends the following parameters to the server. I was able to get access to those parameters by setting the method signature in this format:

    [HttpPost]
    public async Task<ActionResult> GetSubstancesAsync(int draw, int start, int length, Dictionary<string, string> search, List<Dictionary<string, string>> order)

    At the time, I wasn't sure how to access the parameters "search" and "order". I could now access them through a dictionary.

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

    So just to confirm - you are happy that you can get the data you need now?

    Allan

  • steven408steven408 Posts: 9Questions: 3Answers: 1

    Yes. Thank you for your quick response!

This discussion has been closed.