Post parameters to Web API .net core

Post parameters to Web API .net core

BTalayiBTalayi Posts: 18Questions: 4Answers: 0
edited October 2020 in Editor

Hi everyone,

I need to load my table in submitComplete event. here is my code:

   .on('submitComplete', function () {
            table.ajax.url('/api/controller').load();
        });

this works fine but I also want to post one parameter to my controller at server side:

 public ActionResult Find([FromBody] string nameStr)
        {
          ...
        }

is there any way that I can post the parameter here with url?

thanks

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You should just be able to that parameter to the end of the URL string. Alternatively, you could add it in ajax.data - this can also be a function to give you more control,

    Colin

  • BTalayiBTalayi Posts: 18Questions: 4Answers: 0
    edited October 2020

    Hi Colin,

    ajax.data is inside the table configuration and loads the table from the begining. in my case, when user open the page, table should be empty and when they search some names then they want to see the details in the table. so I need to load the table and post parameters to the server side in a button click function. The other option for me is using ajax call which is working for me but before using it I just wanted to know if there is a better way to post parameters to server side out of table configuration.
    here is my ajax call in 'submitComplete' event and it works.

     .on('submitComplete', function () {
                var idString = addedIds.toString();
                $.ajax({
                    type: "POST",
                    url: "/api/namestatus",
                    data: JSON.stringify({ 'pids': idString, 'ticket': ticket }),
                    contentType: "application/json",
                    success: function (response) {
                        var jsonobj = response.data;
                        // draw table using json object
                        table.rows.add(jsonobj).draw();
    
                    },
                    failure: function (xhr) {
                        alert('Request Status: ' + xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText);
                    },
                    error: function (xhr) {
                        alert('Request Status: ' + xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText);
                    }
                });
           });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    when user open the page, table should be empty and when they search some names then they want to see the details in the table

    Using submitComplete for that sounds quite complicated! Could you not just bind an input event handler to the search box input and in that call ajax.reload(). When combined with ajax.data which would read the data from the input, you would get the same effect (just make sure that your server-side script returns an empty array of data when there is no search term - which will be the case initially).

    If you are happy with your approach though, then I don't think there is really a better way of doing it - if you want to POST parameters to your controller, then $.ajax will do that nicely.

    Allan

  • BTalayiBTalayi Posts: 18Questions: 4Answers: 0

    Hi Allan,
    actually it's not only the search, it's a combination of search and add new row.
    so using Ajax call in submit complete worked for me. if I found a better solution I'll post it here.

    Thanks,

This discussion has been closed.