Editor Buttons Not Sending AJAX Requests?
Editor Buttons Not Sending AJAX Requests?
Hi all,
I'm pretty new to web app development and am kind of struggling with getting the Editor buttons to create, edit, or delete. I'm testing around with an ASP.NET app and followed a tutorial online that showed how to get CRUD operations working without the use of Editor, but I'm not a fan of the format.
It uses a single Create new record button at the top of the table and an "Edit" and "Delete" -- I think hyperlink? -- in the far right columns of the table and I'd prefer to have three buttons at the top of the table that can perform those functions. Using this method I can see the AJAX requests going through in my Chrome developer tools, but when I try to use the DataTables Editor buttons to do the same thing, no requests are made.
I assumed it'd be as easy as figuring out how to assign the current working functions assigned to the inline buttons to the DataTables Editor buttons, but I can't seem to.
This is my code. I'm not entirely sure how to use JSFiddle at the moment, but I can try if it'll help more to see the code run.
This question has an accepted answers - jump to answer
Answers
HomeController.cs:
Index.cshtml:
Save.cshtml:
Delete.cshtml
You haven't defined an
ajax
option for the Editor initialisation. So it can't make any Ajax requests - instead it will just do local table editing. You need to useajax
to point it at your CRUD end point.Allan
Hey Allan, thanks for the quick response!
I can see the AJAX requests attempting now, but they're replying with errors. When I try to create a new entry I get a 500 Internal Server Error. I can see that when I use the working button it sends a GET and POST request; whereas, the Editor New button only sends a POST request and the Form data looks more like an array with the [data][0] and it's always [data][0] when I attempt.
I'm assuming it's because I have /home/save/0 as the url in the create option of the ajax part, but even when I remove the 0 it still sends the request as [data][0]?
Correct me if I'm wrong, but I need to find a way to incorporate the row's primary ID into the AJAX request? How would I do that?
Something like this?:
This is the response I get using the Editor New button:
This is the response I get using Add New buttonk:
This is my new Editor initialization:
For edit and delete use
_id_
as shown in this example but for create you would normally just POST to/home/save
(or whatever your route is) since it is the server that typically defines the primary key. It depends upon what your server expects exactly.Allan
I found that example and tried it, but no luck. Maybe it's the way the controller is set up to handle the Http requests. It looks as if each CRUD operation starts with a Get request and then posts. Can I call both a Get and Post request in editor ajax option?
Is the REST interface the best way to go about this or is there an easier method?
I used the DataTables Editor Generator to create a working template that I think I'll build off of.
Thanks a lot, Allan!