user1765862
user1765862

Reputation: 14185

pass parameter to the server using datatable url property in ajax call

I'm using data tables and on initialization inside ajax request I'm setting url using data- attribute

...
 "ajax": {
            "url": $('#mypage').data('source'),
            "type": "GET",
}...

url embeded in page looks like this

 <divid="mypage" [email protected]("CalculateSomething")>
 ...
</dv>

On server side I'm expecting parameter someId

public JsonResult CalculateSomething(int someId)
{
}

my question is:

how can I pass parameter to the server using datatable url property in above ajax call?

update Parameter will come as a value from drop down element inside javascript like

var someId = $('#mydropdown').val();

Upvotes: 4

Views: 3612

Answers (2)

teo van kot
teo van kot

Reputation: 12491

Check documantation on $.ajax. For params you can use data property. Like this:

 url: function(e) { return $('#mypage').data('source'); },
 type: "GET",
 data:
     {
         someId: function(e) { return $('#mydropdown').val(); }, //here is your param
     },
 dataType: "json", //it's better to declare datatype, becouse you using JsonResult in your controller response.

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

There are two ways for this, one is you can concatenate in the url like:

 "ajax": {
            "url": $('#mypage').data('source')+"someId="+id,
            "type": "GET",
}

another way is to use data property of ajax :

"ajax": {
            "url": $('#mypage').data('source')+"someId=123",
            "type": "GET",
            "data": function (data) {

                            data.someId= 123;

                        }
}

Upvotes: 2

Related Questions