Editor Delete & Pass Ajax Route Id Parameter

Editor Delete & Pass Ajax Route Id Parameter

jboldtjboldt Posts: 11Questions: 3Answers: 0
edited July 2017 in Free community support

What's the appropriate way to setup DataTables Editor to pass in the id associated with a record being deleted from the table? For example, the default delete action method and route for a .NET Web API looks like the following example below. However, when setting up the "remove" javascript field with the url it points to the api url but in this case we need to pass the id after the URL instead of returning a form result with embedded JSON data.

// DELETE: api/MyObject/5
[ResponseType(typeof(MyObject))]
public IHttpActionResult DeleteMyObject(long id)

The DataTables ajax javascript is setup as:

remove: {
   type: 'DELETE',
   url: '/api/MyObject/',
}

So what's happening is that I'm receiving the message below because DataTables is trying to pass the action and data to the URL but instead we need to append the row ID to the URL and issue a DELETE instead of passing body form data.

Request URL:http://localhost:51315/api/MyObject/
Request Method:DELETE
Status Code:405 Method Not Allowed

Answers

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin

    Hi,

    You can use _id_ in the URL string and DataTables will replace it automatically with the id for the row (ajax):

    url: '/api/MyObject/_id_'
    

    Worth noting that if you select multiple rows to delete the _id_ string would be replaced with a comma separated list of ids, which I suspect your routing wouldn't match. As such, you probably want to use the removeSingle button.

    Regards,
    Allan

This discussion has been closed.