Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Ajax.ActionLink

For the following:

@Ajax.ActionLink("Delete", "Delete", "AdminGroup", new { id = item.AdminGroupId }, new AjaxOptions { Confirm = "Delete?", HttpMethod = "Delete", OnSuccess = "function() { $(this).parent().parent().remove() }" })

OnSuccess get's errored out. please help. thanks

like image 658
ShaneKm Avatar asked Feb 02 '11 18:02

ShaneKm


1 Answers

It should be like this:

@Ajax.ActionLink(
    "Delete", 
    "Delete", 
    "AdminGroup", 
    new { id = item.AdminGroupId }, 
    new AjaxOptions { 
        Confirm = "Delete?", 
        HttpMethod = "Delete", 
        OnSuccess = "handleSuccess" 
    }
)

where you have:

<script type="text/javascript">
function handleSuccess() {
    // TODO: handle the success
    // be careful because $(this) won't be 
    // what you think it is in this callback.
}
</script>

Here's an alternative solution I would recommend you:

@Html.ActionLink(
    "Delete", 
    "Delete", 
    "AdminGroup", 
    new { id = item.AdminGroupId }, 
    new { id = "delete" }
)

and then in a separate javascript file AJAXify the link:

$(function() {
    $('#delete').click(function() {
        if (confirm('Delete?')) {
            var $link = $(this);
            $.ajax({
                url: this.href,
                type: 'DELETE',
                success: function(result) {
                    $link.parent().parent().remove();
                }
            });
        }
        return false;
    });
});
like image 70
Darin Dimitrov Avatar answered Oct 29 '22 20:10

Darin Dimitrov