为什么MVC Sweet Alert会自动关闭?

时间:2016-07-08 16:50:35

标签: javascript asp.net-mvc sweetalert auto-close

以下是我的编码。请告知为什么甜蜜警报只会弹出不超过1秒然后自动重定向到另一页。

来自HTML:

[HttpPost]
public ActionResult AddSalesTeam(string username, string fullname)
{
    var st = new Sales();

    if (ModelState.IsValid)
    {
        st.Username = username;
        st.FullName = fullname;
        db.User.Add(st);
        db.SaveChanges();
        return RedirectToAction("SalesTeamList");
    }
    return View();
}

来自控制器:

ln -s bin/app.psgi

3 个答案:

答案 0 :(得分:1)

你需要给它一个计时器。

swal({
  title: "Done!",
  text: "Sales Team was successfully added!",
  type: "success",
  timer: 1000, // Wait 1 second
  showConfirmButton: false // No reason to show the button
}, function() {
  window.location.href = '/ABC/STList';
});

答案 1 :(得分:1)

据我所知,您希望显示提醒,然后当用户点击确认按钮,然后重定向到/ABC/STList页面时(相反)自动重定向用户),对吧?如果是这样。你试过这个吗?:

swal({
  title: "Done!",
  text: "Sales Team was successfully added!",
  type: "success",
  showCancelButton: false,
  confirmButtonText: "Ok",   
  closeOnConfirm: false
}, function() {
  window.location.href = '/ABC/STList';
});

========================

编辑:如果需要,可以删除closeOnConfirm: false行,单击确定按钮后将关闭弹出窗口(但您仍将被重定向)。只是因为最终用户更好地看到单击按钮后弹出窗口已关闭。

答案 2 :(得分:0)

在$ ajax函数之前和之后添加e.preventDefault()。

e.preventDefault();  
$.ajax({
  url: "/ABC/AddST/",
  data: {
    "username": $("#username").val(),
    "fullname": $("#fullname").val()
  },
  type: "POST"
}).success(function(data) {
  swal({
    title: "Done!",
    text: "Sales Team was successfully added!",
    type: "success"
  }, function() {
    window.location.href = '/ABC/STList';
  });
}).error(function(data) {
  swal("Oops", "We couldn't connect to the server!", "error");
});
e.preventDefault();