0

I have a datatable which uses a server side datasource like this:

    $('.taskTable').dataTable({
       "bServerSide": true,
       "bProcessing": false,
       "fnServerData": function (sSource, aoData, fnCallback) {
          ...
          aoData.push({ "name": "groupIDs", "value": $("#groupIDs").val() });
          ...
          $.ajax({
             'dataType': 'json',
             'type': 'post',
             'url': "@Url.Action(MVC.Tasks.Tasks.DataTable())",
             'data': aoData
          });
      }
   });

Problem is, $("#groupIDs").val() doesn't format the values so they bind with my model (.NET MVC3). .NET wants to see something like groupIDs[0]=23&groupIDs[1]=42&... but instead it is sending it as groupIDs=[23,42,...]. How can I get datatables to send it in the right format?

2 Answers 2

3

Figured it out. Rather than struggling with getting datatables to auto format it how I wanted, I decided to manually input the values as separate parameters.

"fnServerData": function (sSource, aoData, fnCallback) {
   var groupIDs = $("#groupIDs").val()
   if (groupIDs != null) {
      for (x = 0; x < groupIDs.length; x++) {
         aoData.push({ "name": "groupIDs[" + x + "]", "value": groupIDs[x] });
      }
   }
   ... // more code
}

Works great. Hope this helps someone else.

Sign up to request clarification or add additional context in comments.

Comments

0

groups 23, 42, .... is the equivalent of an array, now I don't know much about .net, however I am sure just like PHP it has a for, while, foreach equivalent of which on your backend you would loop over this array and do with it as you wanted to otherwise.

In this case your example groupdIDs=[23,42] on the back end you would do something like

(note this is PHP I apologize for it not being .net)

foreach($_POST['groupIDs'] as $groupid)
{
   echo $groupid."<br>";
} 

or

for($i=0;$i < count($_POST['groupIDs']);$i++)
{
    echo $_POST['groupIDs'][$i]."<br>";
}

or

echo $_POST['groupIDs'][0]."<br>";
echo $_POST['groupIDs'][1]."<br>";

1 Comment

This is all javascript on the client, not server side. Thanks though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.