我是標簽助手的新手。我有這個
<a href="#" asp-controller="products" asp-action="deleteitem" asp-route-id="@item.Id">Delete</a>
是否有可能首先獲得用戶的確認并將其傳遞給控制器??/動作。例如
<a href="#" asp-controller="products" asp-action="deleteitem" asp-route-id="@item.Id" onclick="DeleteProduct()">Delete</a>
例如,如果 deleteProduct 的結果為真,則動作發生。可能嗎?
uj5u.com熱心網友回復:
可以使用javascript來實作,我這里寫一個簡單的demo
<a asp-controller="Home" asp-action="Index" asp-route-id="@item.Id" id="a1" onclick="DeleteProduct()">Delete</a>
//I am using windows.confirm here to simulate the event,
//when click Ok the event is true, when click cancel the event is false
@section Scripts{
<script>
function DeleteProduct() {
let text = "Press a button!\nEither OK or Cancel.";
if (confirm(text) == true) {
var a = $("#a1").attr('href');
window.Location(a);
} else {
event.preventDefault();
}
document.getElementById("demo").innerHTML = text;
}
</script>
}
結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/443829.html
標籤:asp.net-mvc asp.net 核心 模型视图控制器 标签助手 asp.net-core-tag-helpers
上一篇:用點處理屬性的函式
