2011-02-02 26 views
8

Đối với những điều sau đây: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 get của errored ra. Hãy giúp tôi. nhờ

Trả lời

23

Nó phải là như thế này:

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

nơi bạn có:

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

Dưới đây là một giải pháp thay thế tôi sẽ khuyên bạn:

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

và sau đó trong một se tệp JavaScript parate AJAXify liên kết:

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

có cách nào để trả lại giá trị này "$ (this) .parent(). parent(). remove(); : từ bộ điều khiển và thực thi? – ShaneKm

+0

@Shane, trong bộ điều khiển của bạn, bạn có thể 'trả về JavaScript (" $ (this) .parent(). Parent(). Remove(); ");' nhưng hãy cẩn thận vì '$ (this)' có thể không trỏ đến cái gì bạn có thể mong đợi. Cá nhân tôi sẽ không có bộ điều khiển trả về javascript. Tôi sẽ xử lý nó trong sự gọi lại thành công. –

+0

@Darin, tôi có nên nghĩ rằng Darin không đề xuất Ajax.ActionLink? cảm ơn –