0

I have an ajax link for deleting items in my list.

Here is the view:

@Ajax.ActionLink("Test", "Delete", new { projectID = item.ProjectID }, new AjaxOptions
{
       Confirm = "Are you sure you want to delete this item?",
       HttpMethod = "DELETE",
       OnSuccess = "function() { alert('ok'); }"
})

Here is the action controller:

    [AcceptVerbs(HttpVerbs.Delete)]
    public ContentResult Delete(int projectID)
    {
        Project proj = m_ProjectBusiness.GetProject(projectID);

        if (proj != null)
        {
            m_ProjectBusiness.DeleteProject(proj);
        }

        return null;
    }

The confirmation message is displayed.

The action controller is called.

The view is displayed back

BUT the OnSuccess event is not fired!

Bronzato
  • 9,438
  • 29
  • 120
  • 212

4 Answers4

2

Make sure you have included the following script to your page:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript">         </script>

and that you have enabled unobtrusive javascript in your web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

This is what makes Ajax.* helpers such as Ajax.BeginForm, validations to work.

regards Ajax.BeginForm doesn't call onSuccess

Community
  • 1
  • 1
BrainCoder
  • 5,197
  • 5
  • 30
  • 33
2

Could be that part of the request failed (although would be strange) I would hope its an all-or-nothing process, have your tried implementing the OnFailure Property?

I found a good point on what success means here https://stackoverflow.com/a/1183985/208565 although yours isnt being invoked at all. Would be good to see the status code that is returned if the OnFailure is called.

Community
  • 1
  • 1
Jonathan
  • 2,318
  • 7
  • 25
  • 44
0

I think it should be delegate in C# sense not an implementation:

Check this link http://squarewidget.com/Delete-Like-a-Rock-Star-with-MVC3-Ajax-and-jQuery

Community
  • 1
  • 1
Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • 1
    -1] This answer should have had the pertinent information extracted from the provided link into the answer. That way if the link was to die (it now has) this answer would still be valuable on it's own merit. Unfortunately now, it only serves to be a source of frustration to anyone looking for a complete understanding of the answer. Sorry. – 4imble Oct 25 '18 at 07:56
0

Most probably you don't return a correct response, that ajax interprets and understands. Try using a debugger console to see exactly the reply, and fix it accordingly.

Mihalis Bagos
  • 2,500
  • 1
  • 22
  • 32