MVC3 Ajax.ActionLink

时间:2011-02-02 18:08:45

标签: asp.net-mvc-3

对于以下内容:

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

OnSuccess得到了错误。请帮忙。 感谢

1 个答案:

答案 0 :(得分:23)

应该是这样的:

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

你有:

<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>

这是我建议你的另一种解决方案:

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

然后在单独的javascript文件中AJAX化链接:

$(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;
    });
});
相关问题