So I have an ASP.NET MVC Core project and when I click a button I want to display an alert using sweetalert2.
The button looks like this
<a class="btn btn-primary" onclick="doFunction(@(JsonConvert.SerializeObject(number))); ">Click</a>
And the JavaScript looks like this
function doFunction(model) {
Swal.queue([{
title: 'TestData',
confirmButtonText: 'OK',
text: 'TestData',
showLoaderOnConfirm: true,
preConfirm: () => {
$.ajax({
type: "POST",
url: '@Url.Action("Tester", "Home")',
contentType: "application/json; charset=utf-8",
data: { data: "yourdata" },
dataType: "json",
//success: function(recData) { alert('Success'); },
//error: function() { alert('A error'); }
}).then(Swal.insertQueueStep("Woo", "Woo"))
.catch(() => {
Swal.insertQueueStep({
icon: 'error',
title: 'Something went wrong'
})
});
}
}])
}
And here is the Action
public IActionResult Tester()
{
using (var context = new DbContext(_context))
{
var user = context.Users.FirstOrDefault(x => x.UserName == "TheUser");
return new JsonResult(user.UserName);
}
}
And right now it displays an alert, and when I click "OK" it shows the next alert but no loading window inbetween.
How do I properly call an action that's inside a controller and display a loading alert while it waits for the action to finish, followed by a finished alert.