Problems doing a proper HTTP Delete with Ajax.ActionLink
Asked Answered
T

2

6

What i'm trying to do: Try to delete a record using a "proper" HTTP Delete.

Controller Code:

    [HttpDelete]
    public void DeleteRun(int RunId)
    {
        repository.RemoveEntry(RunId);

    }

Razor View:

             @Ajax.ActionLink("Delete","DeleteRun",new {RunId = run.RunId},
                         new AjaxOptions() { Confirm = "Are you sure you want to delete this entry?",
                                            HttpMethod = "DELETE",
                                            OnComplete = string.Format("DeleteRunInTable({0})",run.RunId)

                         })

Javascript (in separate included file):

   function DeleteRunInTable(RunId) {
       $("tr[data-runid=" + RunId).remove();
}

Link the actionlink method is creating:

 <a data-ajax="true" data-ajax-complete="DeleteRunInTable(11)" data-ajax-confirm="Are you sure you want to delete this entry?" data-ajax-method="DELETE" href="/Runs/Delete/11">Delete</a>

Not sure if the javascript part works yet but not to worried about it. Trying to take it one step at a time :). Now its just working like a traditional tag and when i click the link its just doing a GET request of the href. Of course i get a 404 error because of the [HTTPDelete] i put on my controller. I'm pretty new to web development so i'm sure there are other ways in either javascript or jquery to do the same thing but i'm just doing what i know at this point.

Tumid answered 3/8, 2011 at 21:45 Comment(0)
S
9

This should work as I have done it myself recently and all I had to do was specify the HttpMethod in the AjaxOptions argument.

You also need to ensure you have the jquery.unobtrusive-ajax.js script included on the page.

Sepal answered 3/8, 2011 at 22:25 Comment(3)
Thanks for the response... No actually i deleted them. I thought they weren't needed. I thought the ajax.actionlink would use jquery. Am i wrong? If i remember correctly on pluralsight i was watching one of the videos with K Scott Allen and he deleted both the microsrosoft ajax scripts when he was using ajax.actionslink. I'm pretty novice at this so maybe actionlink uses microsoft ajax scripts in certain cases and in other cases it doesn't. Not sure :)Tumid
Thanks it was my fault. The microsoft ajax scripts where not needed it appears. I missed one script in the page that i was unaware of: jquery.unobtrusive-ajax.min.js. Probably my next question here will probably be if there is a good way to tell when you have a script missing from the page instead of it just (not working) :P.Tumid
ahh cool, I had a feeling it was a missing script... just wasn't the right one :) I've updated my answer to reflect the correct script in case future users have the same problem and don't read the comments.Sepal
T
4

It was actually a simple solution....i was missing the jquery.unobtrusive-ajax.min.js :P. I'm leaving the post here so anyone trying to do something similar to what i'm doing will know its possible just make sure you include jquery & jquery.unobtrusive.

Edit: Just to clarify ActionLink works with JQuery if your using MVC3 otherwise it uses the microsoft javascript libraries.

Tumid answered 3/8, 2011 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.