razor - ASP.NET Core 2.2 剃刀视图中的操作按钮
问题描述
假设我们正在重新设计下面的视图:
当前代码是:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Url)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.BlogId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.BlogId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.BlogId">Delete</a>
</td>
</tr>
}
我们的目标是删除Delete
每一行中链接的视图。相反,我们希望有一个删除按钮,在确认后删除行而不离开Index
页面(重新加载很好)。
如何实现这样的按钮?是否有任何相关文件?
解决方案
您可以使用 ajax 发出删除请求,以便用户可以在删除过程中停留。ajax 请求成功后,您可以从 UI 中删除表格行。
首先,为您的锚标记添加一个新属性,您可以使用该属性为您的 jQuery 选择器连接点击事件。
<a asp-action="Delete" ajaxy asp-route-id="@item.BlogId">Delete</a>
在这里,我添加了一个名为ajaxy
现在我们将click
使用该属性监听锚标记上的事件ajaxy
,停止正常行为(导航到 href 属性值 URL),而是进行 ajax 调用。要进行确认,您可以使用window.confirm
API。
@section Scripts
{
<script>
$(function () {
$("a[ajaxy]").click(function (e) {
e.preventDefault();
if (window.confirm("Are you sure ?")) {
var $this = $(this);
var url = $this.attr("href");
$.post(url).done(function (res) {
$this.closest("tr").fadeOut(300, function (a) {
$(this).remove();
});
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("error in ajax call!" + errorThrown);
})
}
});
});
</script>
}
假设您的Delete
操作接受Id
参数。
[HttpPost]
public async Task<IActionResult> Delete(int id)
{
// to do : return something
}
推荐阅读
- javascript - 将光标悬停在图像上后 - 箭头必须出现在左侧和右侧
- javascript - 自定义图像库上的图像闪烁
- javascript - 如何在我的对象标签的内屏幕上获得与输入网站控制台命令相同的效果?
- javascript - Quasar:当我单击其中一个菜单时,drawerClick 不起作用
- sql - 查询每个品种每分钟数据的差异
- assembly - 仅装配数字输入
- xml - 无法为安装 WiX 设置自定义位置。安装后,它只安装在 C\programfiles86 中。它只有 bundle.wxs 没有 product.wxs
- python - 如何在Android设备的下载文件夹中找到文件的完整路径(然后使用python枕头打开)?
- vmware - 在 AMD Ryzen 5 3500 上尝试 ESXi 6 / 6.5 时发现错误“无网络适配器”
- gcc - 测量全局变量的内存使用情况