1

我在控制器中有这个方法

[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 函数。任何的想法?

谢谢

4

1 回答 1

4

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

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

Also you'd better use OnSuccess = "deleteSuccess" instead of OnComplete = "deleteComplete" because you should update only if the Delete call actually succeeded. Don't forget that the OnComplete callback is always invoked, no matter whether the AJAX call succeeded or not.

于 2013-01-27T15:15:30.197 回答