成功发布后刷新剑道网格表



我目前正在使用剑道网格作为ASP.Net核心MVC。

因此,我创建了一个列,其中包含一组选项。

columns.Template("<div class='btn-group'></div>")
.Title("").Width(100);

然后使用jquery:

$(this).find(".btn-group").append("<button type='button' class='btn btn-light btn-sm dropdown-toggle' data-bs-toggle='dropdown' aria-expanded='false'>Actions<i class='mdi mdi-chevron-down'></i>. 
</button>
<ul class='dropdown-menu dropdown-menu-end'>
<li>
<a class='dropdown-item text-danger is-active' onclick='showSweetAlert(" + dataItem.AdvertiserId+", " + dataItem.IsActive +")' type='button'>Mark Inactive</a>
</li>
</ul>");

正如你所看到的,我使用onclick函数来显示Sweet Alert 2库

代码:

function showSweetAlert(id, isActive){
if (isActive) {
Swal.fire({
title:"Are you sure?",
text:"This will inactivate advertiser!",
icon:"warning",
showCancelButton:!0,
confirmButtonColor:"#2ab57d",
cancelButtonColor:"#fd625e",
confirmButtonText:"Inactivate",
}).then(function(result){
if(result.isConfirmed){
$.ajax({
url:'/Advertisers/ActiveAdvertiser?id='+id+'&isActive='+!isActive,
method: 'POST',
success: function(r){
Swal.fire("Inactivated!", "Advertiser inactivated successfully", "success");
},
error: function (request) {
console.log(request.responseText)
Swal.fire("Error!", "Something went wrong, please try again`", "warning");
// window.location.href ='@Url.Action("Index","Advertisers")'
}
});
}
})
}

所以,如果执行了按钮,我会创建一个修改值的post操作,当它完成服务时,如果效果良好,则返回OKBadRequest。如果成功,它将显示一个新的警报,然后您可以关闭模态。

我想实现的是一种刷新网格以获得新值的方法,有可能实现吗?

控制器:

[HttpPost]
public async Task<JsonResult> ActiveAdvertiser(int id, bool isActive)
{
var advertiser = await _advertisersService.GetAdvertiserByAdvertiserIdAsync(id);
if (advertiser == null) return Json(new { result = "BadRequest" });
var model = AssignAdvertiserViewModel(advertiser, id);
model.IsActive = isActive;
var result = await _advertisersService.UpdateAdvertiserAsync(model, GetCurrentUserAsync().Id);
return Json(result != null ? new { result = "OK" } : new { result = "BadRequest" });
}

ASP.NET MVC的Telerik UI网格在内部实现了Kendo UI网格公开的所有功能。因此,您可以使用网格的refresh((JavaScript方法:

$("#grid").data("kendoGrid").refresh(); // grid should be replaced with the Name of your grid

或者,可以通过调用数据源的read((方法直接刷新数据源。

$("#grid").data("kendoGrid").dataSource.read(); // grid should be replaced with the Name of your grid

最新更新