微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET Core,对Delete的Ajax调用不会从html json响应中重新加载索引页PartialView中的div内容

如何解决ASP.NET Core,对Delete的Ajax调用不会从html json响应中重新加载索引页PartialView中的div内容

我在Index.chtml中定义了一些简单的代码,以列出到目前为止已完成的所有收集工作。

@model IEnumerable<PaymentTracking.Models.PaymentCollection>
<div id="view-all">
    @await Html.PartialAsync("_ViewAll",Model)
</div>

_ViewAll.chtml

@model IEnumerable<PaymentTracking.Models.PaymentCollection>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.displayNameFor(model => model.AirlineCode)
            </th>
            <th>
                @Html.displayNameFor(model => model.PaymentCollectionAmount)
            </th>
            <th>
                @Html.displayNameFor(model => model.PaymentCollectionForDate)
            </th>
            <th>
                @Html.displayNameFor(model => model.PaymentCollectionRemarks)
            </th>
            <th>
                @Html.displayNameFor(model => model.CreatedUser)
            </th>
            <th>
                @Html.displayNameFor(model => model.CreatedDt)
            </th>
            <th> 
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.displayFor(modelItem => item.AirlineCode)
                </td>
                <td>
                    @Html.displayFor(modelItem => item.PaymentCollectionAmount)
                </td>
                <td>
                    @Html.displayFor(modelItem => item.PaymentCollectionForDate)
                </td>
                <td>
                    @Html.displayFor(modelItem => item.PaymentCollectionRemarks)
                </td>
                <td>
                    @Html.displayFor(modelItem => item.CreatedUser)
                </td>
                <td>
                    @Html.displayFor(modelItem => item.CreatedDt)
                </td>
                <td>
                        <a class='btn btn-danger text-white' style='cursor:pointer; width:70px;' onclick="Delete('/PaymentCollection/Delete?id=@item.PaymentCollectionID')"> Delete </a>


                </td>
            </tr>
        }
    </tbody>
</table>

JavaScript句柄删除操作

function Delete(url) {
    swal({
        title: "Are you sure?",text: "Once deleted,you will not be able to recover",icon: "warning",buttons: true,dangerMode: true
    }).then((willDelete) => {
        if (willDelete) {
            $.ajax({
                type: "DELETE",url: url,dataType: 'json',contentType: "application/json; charset=utf-8",success: function (data) {
                    if (data.success) { 
                        $("#view-all").html(data.html);
                        toastr.success(data.message);    
                    }
                    else {
                        toastr.error(data.message);
                    }
                }
            });
        }
    });
}

我的控制器的操作方法

        [HttpDelete]

        public async Task<IActionResult> Delete(int id)
        {
            var pcolFromDb = await _db.PaymentCollections.FirstOrDefaultAsync(u => u.PaymentCollectionID == id);
            if (pcolFromDb == null)
            {
return Json(new { success = false,message = "Error while Deleting",isValue = false,html = Helper.RenderRazorViewToString(this,"Upsert",PaymentCollection) });
            }
            _db.PaymentCollections.Remove(pcolFromDb);
            await _db.SaveChangesAsync();



return Json(new { success = true,message = "Delete successful",isValid = true,html = "successful"});
        }

删除后,我得到了响应,并看到成功的烤面包机消息,但是<div id="view-all">内容没有重新加载从Ajax返回的html内容

下面是删除按钮后返回的屏幕截图

enter image description here

您介意指出我做错了哪些点吗?

解决方法

问题与缓存有关,清除所有缓存后,问题中的初始代码发布按预期工作。

谢谢您指出了调试的好方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。