如何使UpdateTargetId在Ajax.ActionLink中工作?

时间:2013-01-27 15:05:56

标签: c# ajax jquery

我在控制器中有这个方法

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

在视图中我有这个,调用一个返回局部视图的方法

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

GetDocumentsByMember方法

    public ActionResult GetDocumentsByMember()
    {
        var companyGuid = HttpContextHelper.GetUserCompanyGuid();

        var documents = _service.GetUploadedDocumentsByMember(companyGuid);

        return PartialView(documents);
    }

部分视图

@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函数。有什么想法吗?

由于

1 个答案:

答案 0 :(得分:4)

您可以使用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调用是否成功。