Search code examples
asp.net-mvcasp.net-coremodel-view-controllertag-helpersasp.net-core-tag-helpers

getting confirmation first from user in a Tag helper


I am new to Tag Helper. I have this

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

is it possible first get the confirmation of user and pass it controller/Action. for example

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

for example if the result of deleteProduct is true the action takes place. is it possible?


Solution

  • You can use javascript to achieve this, I write a simple demo here

    <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>
    }
    

    result

    enter image description here