MVC3 Ajax.ActionLink [英] MVC3 Ajax.ActionLink

查看:138
本文介绍了MVC3 Ajax.ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关以下内容:

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

的onSuccess获取的出错了。请帮忙。
谢谢

OnSuccess get's errored out. please help. thanks

推荐答案

这应该是这样的:

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


下面是一个替代解决方案,我会建议你:


Here's an alternative solution I would recommend you:

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

,然后在一个单独的JavaScript文件AJAXify链接:

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;
    });
});

这篇关于MVC3 Ajax.ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆