MVC3 Ajax.ActionLink

前端 未结 1 1798
温柔的废话
温柔的废话 2021-02-03 13:08

For the following:

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


        
相关标签:
1条回答
  • 2021-02-03 13:55

    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;
        });
    });
    
    0 讨论(0)
提交回复
热议问题