Search code examples
asp.net-mvcdatatablesget

DataTables always sends GET request instead of POST


My DataTables is unwilling to send POST request to my ASP.NET MVC Action. I tried to search Internet but nothing. Here is teh code:

            $(document).ready(function () {
            let token = $('input[name="__RequestVerificationToken"]', this).val();

            let table = $('#hit').DataTable({
                "destroy": true                                             
                , "proccessing": true                                       
                , "pagingType": "full_numbers"                              
                , "stateSave": true                                         
                , "serverSide": true                                        
                , "orderCellsTop": true                                     
                , "fixedHeader": true                                       
                , "ajax": {                        
                    "url": @Html.Action("LoadData", "Home"),    
                    "type": "POST"
                    "data": {
                        __RequestVerificationToken: token
                    }
                }
                , "fixedColumns": true                                      
                , "order": [[1, 'asc']]
                ...

and on the server:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult LoadData(DataTableParameters requestParameters)
    {
        return Json(service.GetPagedData(requestParameters));
    }

and the error message:

A public action method 'LoadData' was not found on controller 'Project.Web.Controllers.HomeController'.

How can I fix this issue, please.


Solution

  • The most possible cause is usage of @Html.Action() helper which trying to call the controller action with GET method:

    "url": @Html.Action("LoadData", "Home"),
    

    You should use @Url.Action() instead, which generates URL string instead of calling controller action directly:

    "url": '@Url.Action("LoadData", "Home")',