Using JQuery DataTables with ASP.NET MVC 5

2

I have questions about how to use DataTables with the MVC 5 and submit button by returning a Json as per the site: DataTables

$(document).ready(function() {
$('#example').dataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "scripts/post.php",
        "type": "POST"
    },
    "columns": [
        { "data": "first_name" },
        { "data": "last_name" },
        { "data": "position" },
        { "data": "office" },
        { "data": "start_date" },
        { "data": "salary" }
    ]
} );} );
  • Use MVC instead of php
  • How should I put the call to the MVC ActionResult and return a JSON as the standby component.
  • I wanted to put fields to filter example Name Address and get down to the controller these two fields and make a call to return benchmark result I do not know how to convert to Json that the component waits:

    {
      "draw": 1,
      "recordsTotal": 57,
      "recordsFiltered": 57,
      "data": [
        {
          "first_name": "Airi",
          "last_name": "Satou",
          "position": "Accountant",
          "office": "Tokyo",
          "start_date": "28th Nov 08",
          "salary": "$162,700"
        },
        {
          "first_name": "Cedric",
          "last_name": "Kelly",
          "position": "Senior Javascript Developer",
          "office": "Edinburgh",
          "start_date": "29th Mar 12",
          "salary": "$433,060"
        }
      ]
    }
    
  • asked by anonymous 14.11.2014 / 02:49

    1 answer

    4

    It does not change much the way it is done in PHP. When describing Ajax:

    "ajax": {
        "url": "scripts/post.php",
        "type": "POST"
    },
    

    View to write the JS script as follows:

    "ajax": {
        "url": @Url.Action("MinhaAction", "MeuController"),
        "type": "POST"
    },
    

    @Url.Action accepts multiple forms to be called . Once this is done, declare an Action on your Controller as follows:

    [HttpPost]
    public JsonResult MinhaAction(FormCollection formCollection) 
    {
        // Leia os dados de formCollection aqui (é um dicionário).
        // Retorne um JSON para seu AJAX usando JSON.NET.
    
        return Json(meusObjetos, JsonRequestBehavior.AllowGet);
    }
    

    meusObjetos can user an object or a collection of them. JSON.NET knows how to serialize objects correctly in JSON.

    To install JSON.NET, use NuGet:

      

    link       }

        
    14.11.2014 / 07:40