How do you make a call to MVC Web API from Javascript for Datatables?????

How do you make a call to MVC Web API from Javascript for Datatables?????

JArmbrusterJArmbruster Posts: 5Questions: 1Answers: 0

JavaScript call:

      $('#example').DataTable( {
          "ajax": { url: "/api/PatientData/SearchPatientRecord",
              "data": { 'patientName': "Smith", 'insuranceNumber': "99" },
              "type": "POST"
                   },
        "serverSide":true,
        "columns": [
        { "data": "comp_name" },
        { "data": "addr_Address1" },
        { "data": "addr_State" },
        { "data": "pers_FirstName" },
        { "data": "pers_LastName" },
        { "data": "pers_Gender" }
        ]
       } );

C# Web API method:

    [HttpPost]
    [Route("SearchPatientRecord")]
    public string SearchPatientRecord(HttpRequestMessage request, [FromBody]List<string> value)
    {

        //PatientInsuranceRecords pRcrds = new PatientInsuranceRecords();
        //return JsonConvert.SerializeObject(pRcrds.SearchPatientRecordsWM(patientName, insuranceNumber));

        return "Test";
    }

The call is received, but I cam not see the parameters that are passed, including the parameters passed by DataTables.

Help?

Jeff

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    "serverSide":true,

    Do you really need that? Are you working with tens of thousands of records or more? If you are, you'd need to implement server-side processing at the server.

    Allan

  • JArmbrusterJArmbruster Posts: 5Questions: 1Answers: 0

    Allan,

    I actually have over 100,000 records (patients) for a medical company.

    The web page allows user to search and edit patient records. Table shows search results.

    So user enters patient name or insurance number. If name, web page needs to show list of found names.

    So the question is, what are the steps to using DataTables to display the info?

    I assume this:

    Call function:

    JavaScript code:

        var patientLName = $("#patientLastName").val();
          var insuranceNumber = $("#insuranceNumber").val();
    
          $('#example').DataTable( {
              "ajax": { url: "/api/PatientData/SearchPatientRecord",
                  "data": { 'patientName': patientLName, 'insuranceNumber': insuranceNumber      },
                  "type": "POST"
                       },
            "serverSide":true,
            "columns": [
            { "data": "comp_name" },
            { "data": "addr_Address1" },
            { "data": "addr_State" },
            { "data": "pers_FirstName" },
            { "data": "pers_LastName" },
            { "data": "pers_Gender" }
            ]
           } );
    

    But DataTables passes a long list of parameters.

    Web API code:

        [HttpPost]
        [Route("SearchPatientRecord")]
        public string SearchPatientRecord(HttpRequestMessage request, [FromBody]List<string> value)
        {
    
            PatientInsuranceRecords pRcrds = new PatientInsuranceRecords();
            return JsonConvert.SerializeObject(pRcrds.SearchPatientRecordsWM(patientName, insuranceNumber));
    
        }
    

    But I can't seem to grab the parameters that are being passed by DataTables???????

    Help????

    Jeff

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin
    Answer ✓

    I actually have over 100,000 records (patients) for a medical company.

    Server-side processing it is then :smile:.

    But DataTables passes a long list of parameters.

    Yes - those parameters tell the server what data DataTables wants to display (number of records in a page, page start, any filtering, etc).

    Exactly how you access that information is really a server-side issue. It looks like you have the information from the POST request in the value property, and you'd need to take the data from there - the Microsoft .NET documentation would be where to find out how to do that. Possibly simply value["draw"], etc, but I'm honestly not sure as I'm not familiar with FromBody.

    There is another option - you could try using Editor's .NET implementation, which does fully support server-side processing. Although Editor is paid for software, you don't need a license if you are only going to use the server-side library and not the client-side javascript.

    Allan

This discussion has been closed.