0

Below are my coding. Please advise why the sweet alert only pop up not up to 1 second then auto Redirect to another page.

From HTML:

$.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");
});

From Controller:

[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();
}
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
Tss
  • 88
  • 4
  • 13

3 Answers3

1

You need to give it a timer.

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';
});
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
1

As far as I understand from the question, you want to show the alert, and then when the user click in the confirm button, then redirect to the /ABC/STList page, (instead of redirecting the user automatically), right? If so. have you tried with this?:

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

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

EDIT: If you want, you can remove the closeOnConfirm: false line and the popup will be closed after clicking the ok button (but you will be still redirected). Is just because is better for the End User to see that the popup is closed after clicking the button.

sebaferreras
  • 44,206
  • 11
  • 116
  • 134
0

Adding e.preventDefault() before and after $ajax function will do.

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(); 
Tss
  • 88
  • 4
  • 13