最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SweetAlert Confirm prompt with @Html.ActionLink - Stack Overflow

programmeradmin6浏览0评论

I am using SweetAlert2 to replace javascript alerts in my MVC5 app. My question is: How do I use the sweetalert confirmation before a delete action is run. For example, this works fine....

 <span onclick="return confirm('Are you sure to delete?')">
        @Html.ActionLink("Delete", "Delete", new { roleName = @role.Name }, new { @class = "btn btn-success btn-xs" })
   </span>

If I cancel the delete action is not run. If I click ok it's run properly.

But I want to use SweetAlert2. Basically here's the prompt....

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function () {
  swal(
    'Deleted!',
    'Deleted.',
    'success'
  )
})

The problem is that I'm not sure how to replace the confirm with this code and have it work correctly.

I tried wrapping the above code in a function and returning true if it was a success, but the problem is the ActionLink action is always run regardless of if I cancel or not.

I am using SweetAlert2 to replace javascript alerts in my MVC5 app. My question is: How do I use the sweetalert confirmation before a delete action is run. For example, this works fine....

 <span onclick="return confirm('Are you sure to delete?')">
        @Html.ActionLink("Delete", "Delete", new { roleName = @role.Name }, new { @class = "btn btn-success btn-xs" })
   </span>

If I cancel the delete action is not run. If I click ok it's run properly.

But I want to use SweetAlert2. Basically here's the prompt....

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function () {
  swal(
    'Deleted!',
    'Deleted.',
    'success'
  )
})

The problem is that I'm not sure how to replace the confirm with this code and have it work correctly.

I tried wrapping the above code in a function and returning true if it was a success, but the problem is the ActionLink action is always run regardless of if I cancel or not.

Share Improve this question asked Feb 20, 2017 at 21:22 WebDevGuy2WebDevGuy2 1,2591 gold badge23 silver badges45 bronze badges 1
  • You should make your link empty and should call Delete after confirmation from script – Akash KC Commented Feb 20, 2017 at 21:47
Add a ment  | 

1 Answer 1

Reset to default 7

First of all, your current code is navigating to the delete action. Any action methods which is altering data should not be an Http GET action method. It should be inside a Http Post action method.

[HttpPost]
public ActionResult Delete(string roleName)
{
    // to do  : Delete and return something
}

Now since our Delete method is HttpPost, you need a form submit, not navigating to a link via browser(which is a GET request). So build a form tag around your delete button(keep the roleName in a hidden field in the form), listen to the click event on this button, prevent the normal behavior which is navigating to the new url, instead, show the sweet alert, and in the then callback (user confirmed "Yes"), submit the form.

@using (Html.BeginForm("Delete", "Home"))
{
    <input type="hidden" name="roleName" value="admin" />
    <span>
        @Html.ActionLink("Delete", "Delete", null,
                           new { @class = "btn btn-success btn-xs deleteBtn" })
    </span>
}

And the javascript

$(function () {

    $(".deleteBtn").click(function (e) { 
        //whenever our button is clicked

        e.preventDefault();  
        // stop the default behavior(navigation)
        var _form = $(this).closest("form");
        //get a reference to the container form 

        swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then(function () {
            //user selected "Yes", Let's submit the form
            _form.submit();
        });

    });

})
发布评论

评论列表(0)

  1. 暂无评论