0
votes

I have been tearing my hair out over this one. I have two projects one running ASP.NET 4 and the other ASP.NET 5 RC1

The ASP.NET 5 project controller received POST method input parameters are all default and not the values as sent from the webpage.

To narrow down the problem I simplified the controllers POST methods in both projects

[HttpPost]
public JsonResult DataHandler(int Draw)
{
    //above Draw variable not set
}

and put a break point on the method to catch the variable Draw. The webpage sends a JSON post with a value of 1 for the Draw parameter. However in ASP.NET 5 that values is 0 (default) and other parameters I send are null instead of having values. In 'ASP.NET 4' it is correct.

I am using jquery datatables and the same code as used in this ASP.NET 4 project

var oTable = $('#datatab').DataTable({
    "serverSide": true,
    "ajax": {
        "type": "POST",
        "url": '/Home/DataHandler',
        "contentType": 'application/json; charset=utf-8',
        'data': function (data) { return data = JSON.stringify(data); }
    },
    "dom": 'frtiS',
    "scrollY": 500,
    "scrollX": true,
    "scrollCollapse": true,
    "scroller": {
        loadingIndicator: false
    },
    "processing": true,
    "paging": true,
    "deferRender": true,
    "columns": [
   { "data": "Name" },
   { "data": "City" },
   { "data": "Postal" },
   { "data": "Email" },
   { "data": "Company" },
   { "data": "Account" },
   { "data": "CreditCard" }
    ],
    "order": [0, "asc"]
});

I used Fiddler and compared the JSON sent by both project to the controller and the JSON content posted to the /Home/DataHandler for both are the exact same i.e. Draw=1.

{"draw":1,"columns":[{"data":"Name","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"City","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Postal","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Email","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Company","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"Account","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}},{"data":"CreditCard","name":"","searchable":true,"orderable":true,"search":{"value":"","regex":false}}],"order":[{"column":0,"dir":"asc"}],"start":0,"length":126,"search":{"value":"","regex":false}}

Things I tried.

  • I used the same html table contents and above code .js file between projects
  • Set my controller input parameter to lowercase e.g. draw
  • Compare the JSON POST data in fiddler is the same
  • Put a breakpoint on the POST method input variable to catch the value as soon as it is posted
1
Try removing contentType and data from the ajax section of the DataTable setup.Jamie Dunstan
Try adding the [FromBody] attribute as in this answerDaniel J.G.
Thanks Daniel. That did the trickdevfric

1 Answers

0
votes

Add dataType: 'json' to the ajax call

Replace return data = JSON.stringify(data) with the following:

return data.Draw = JSON.stringify(data)