Extra Ajax Parameter - how do I access it in the controller ?

Extra Ajax Parameter - how do I access it in the controller ?

duggieduggie Posts: 5Questions: 2Answers: 0

I'm trying to add extra parameters to my jquery request in datatables. I've seen that I can add parameters in the ajax part of the request, but when the request hits the controller, the object type is DefaultDataTablesRequest which does not seem hold additional parameters. (My project is ASP.NET MVC5). Here is my jquery request from the view :-

 var resultVM;
        $(function () {
            resultVM = {
                dt: null,

                init: function () {
                    dt = $('#results-table').DataTable({
                        "serverSide": true,
                        "processing": true,
                        "ajax": {
                            "url": "@Url.Action("GetResults","Result")",
                            "data": function (d) {
                                d.Percentage = "50"
                            }
                        },
                        "columns": [
                            { "title": "SITE NAME", "data": "site_name", "searchable": true },
                            { "title": "IP ADDRESS", "data": "ip_address", "searchable": true },
                            { "title": "SPEED RESULT (mbps)", "data": "speed_result", "searchable": true },
                            { "title": "DIRECTION", "data": "direction", "searchable": true },
                            { "title": "TEST DATE/TIME", "data": "test_datetime", "searchable": true },
                            { "title": "LINESPEED - DOWN", "data": "linespeed_down", "searchable": true },
                            { "title": "LINESPEED - UP", "data": "linespeed_up", "searchable": true }
                        ],
                        "lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
                        "pagingType": "full_numbers"
                    });
                }
            }

            // initialize the datatables
            resultVM.init();
        });

I've added an additional parameter in there :-1:

"data": function (d) {
          d.Percentage = "50"
}

but that doesn't get passed to the controller, what else am I missing ? Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Request.Form.Percentage should be all you need I think there.

    Allan

  • duggieduggie Posts: 5Questions: 2Answers: 0

    Thanks for the response Allan, but I can't see that option in the controller, visual studio doesn't show "Percentage" in Request.Form.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Oops - Request.Form["Percentage"].

    Allan

  • duggieduggie Posts: 5Questions: 2Answers: 0

    OK getting closer, but that value is showing as null in the controller.

    Is the syntax in the jquery script correct ? Thanks.

  • duggieduggie Posts: 5Questions: 2Answers: 0

    Hi Allan, I found the issue, I didn't have the request set to POST, once I changed that I could see the parameter correctly, thanks for the help.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Perfect - thanks for posting back :).

    Allan

This discussion has been closed.