Skip to main content
 首页 » 编程设计

c#之如何使 UpdateTargetId 在 Ajax.ActionLink 中工作

2024年02月20日30EasonJim

我在 Controller 中有这个方法

[HttpDelete] 
public void DeleteDocument(int id) 
{ 
   //Here I do the deletion in the db 
} 

在我的 View 中,调用一个返回局部 View 的方法

@{ Html.RenderAction("GetDocumentsByMember"); } 

GetDocumentsByMember 方法

    public ActionResult GetDocumentsByMember() 
    { 
        var companyGuid = HttpContextHelper.GetUserCompanyGuid(); 
 
        var documents = _service.GetUploadedDocumentsByMember(companyGuid); 
 
        return PartialView(documents); 
    } 

和局部 View

@model IEnumerable<GradientCapital.DomainModel.Entity.Document.Document> 
<div id="uploadeddocuments"> 
    @*Here there's a table and at one of the columns there's the next link*@ 
 
    <td id="delete"> 
        @Ajax.ActionLink("Delete", "DeleteDocument", new { id = document.Id }, 
        new AjaxOptions 
            { 
               Confirm = "Are you sure you want to delete?", 
               HttpMethod = "DELETE", 
               OnComplete = "deleteComplete" 
            }) 
    </td> 
</div> 

deleteComplete 只是刷新一切

<script type="text/javascript"> 
    function deleteComplete() { 
        window.location.reload(); 
    } 
</script> 

一个简单问题的代码很长(格式是否正确?),我无法使 ajaxoption UpdateTargetId 在这里工作,而不必调用此 deleteComplete 函数。有什么想法吗?

谢谢

请您参考如下方法:

无需重新加载整个页面,您可以使用 AJAX 调用 GetDocumentsByMember 操作并仅更新实际更改的 DOM 部分:

<script type="text/javascript"> 
    function deleteComplete() { 
        $.ajax({ 
            url: '@Url.Action("GetDocumentsByMember")', 
            type: 'GET', 
            cache: false, 
            success: function(result) { 
                $('#uploadeddocuments').html(result); 
            } 
        }); 
    } 
</script> 

此外,您最好使用 OnSuccess = "deleteSuccess" 而不是 OnComplete = "deleteComplete",因为只有在 Delete 调用实际成功时才应该更新。不要忘记 OnComplete 回调总是被调用,无论 AJAX 调用是否成功。