确认后导航到 url

Navigate to url after confirmation

正在开发 MVC 5 应用程序。

我有一个按钮定义为....

  @Html.ActionLink("Delete Chart " + Model[0].CurrentChartNumber,                                               
          "DeleteChartData",
          new { eventId = Model[0].EventId, 
                chartNumber = Model[0].CurrentChartNumber },
          new { @class = "btn btn-danger btnDelete" })

呈现为...

<a class="btn btn-danger btnDelete" 
   href="/AuditResults/DeleteChartData?eventId=50&amp;chartNumber=1">
   Delete Chart 1
</a>

我正在使用 sweetAlert 进行删除确认。一切正常,但是,在用户单击“是”后,我无法让它导航到定义的 href。这是 javascript 代码...

<script>
$(function() {
    $(".btnDelete").click(function(e) {
        e.preventDefault();
        swal({
            title: 'Are you sure you want to DELETE this chart?',
            text: "You won't be able to undo this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then(function() {
            //user selected "Yes"
            return true;
        });

    });

})
</script>

所以,如果用户点击是,它应该导航到....

/AuditResults/DeleteChartData?eventId=50&amp;chartNumber=1    

...但它不会导航到任何地方。 (我什至删除了 e.preventDefault() 并尝试了它。)知道我做错了什么吗?谢谢!

当用户选择是时,您除了从 promise 返回 true 之外什么也没做。因此,请尝试在代码块内将重定向添加到您想要的页面。

<script>
$(function() {
    $(".btnDelete").click(function(e) {
        e.preventDefault();
        swal({
            title: 'Are you sure you want to DELETE this chart?',
            text: "You won't be able to undo this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then(function() {
            //user selected "Yes"
            window.location.replace("AuditResults/DeleteChartData?eventId=50&amp;chartNumber=1");
            return true;
        });

    });

})
</script>