Bronzato
Bronzato

Reputation: 9362

Event OnSuccess on an ajax call is not fired

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!

Upvotes: 0

Views: 4602

Answers (4)

BrainCoder
BrainCoder

Reputation: 5337

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

Upvotes: 2

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

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

Upvotes: 0

Mihalis Bagos
Mihalis Bagos

Reputation: 2510

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.

Upvotes: 0

Jonathan
Jonathan

Reputation: 2358

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.

Upvotes: 2

Related Questions