在 Tag 助手中首先从用户那里获得确认

getting confirmation first from user in a Tag helper

我是 Tag Helper 的新手。我有这个

<a href="#" asp-controller="products" asp-action="deleteitem" asp-route-id="@item.Id">Delete</a>

可不可以先得到用户的确认再通过controller/Action。例如

<a href="#" asp-controller="products" asp-action="deleteitem" asp-route-id="@item.Id"  onclick="DeleteProduct()">Delete</a>

例如,如果 deleteProduct 的结果为真,则操作发生。可能吗?

可以用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>
}

结果