I work on asp.net mvc Application Ajax Request Calling I face Issue message sweet alert display more than once if i click button submit more than once
if i press same button submit again for same request it will display (message Employee Exist Before) two time
if i press same button submit button again for same request it will display message validation (message Employee Exist Before) 3 time
correct or expected behavior
when click submit button and employee exist before then show message validation (message Employee Exist Before) one time only .
why that happen and how to prevent display same message multi time
$("#ResignationApp").submit(function (e) {
e.preventDefault();
var formData = $(this).serialize();
console.log("data is" + formData)
$.ajax({
type: "POST",
dataType: 'json',
url: '@Url.Action("RequesterIndex", "Resignation")',
data: formData,
success: function (response) {
for (let item of response) {
if (item.Key === "success") {
success = item.Value;
}
if (item.Key === "message") {
message = item.Value;
}
}
if (success) {
Swal.fire({
icon: 'success',
title: 'Submition Request',
text: message
}).then((result) => {
if (result.isConfirmed) {
var url = '@Url.Action("IndexResignation", "Home")' + '?filenumber=' + empidval;
window.open(url, '_self');
}
});
} else {
Swal.fire({
icon: 'error',
title: 'Resignation Submission Form',
text: 'Employee Exist Before'
});
return false;
}
},
error: function (error) {
var url = '@Url.Action("UnauthorizedUser", "Home")' + '?filenumber=' + empidval;
window.open(url, '_self');
}
});
});
public JsonResult RequesterIndex(ResignationRequester resignationRequester)
{
dynamic responseData = new ExpandoObject();
responseData.success = false;
responseData.message = "";
var filenumber = resignationRequester.EmpID;
if (Session[SessionKeys.UserCode] != null)
{
JDEUtility jde = new JDEUtility();
if (ModelState.IsValid)
{
int checkEmployeeNoExist = jde.CheckEmployeeExistOrNot(resignationRequester.EmpID);
if (checkEmployeeNoExist >= 1)
{
responseData.success = false;
responseData.message = "Employee Exist Before";
return Json(responseData);
}
try
{
Workforce.InsertToReignation(resignationRequester, JoinedDate, (string)Session[SessionKeys.Username], (DateTime)Session[SessionKeys.LastWorkingDate], noticeperiod, (int)Session[SessionKeys.UserCode]);
}
catch (Exception ex)
{
responseData.success = false;
responseData.message = "Create Not Done Correctly";
return Json(responseData);
}
if (string.IsNullOrEmpty(ViewBag.errorMsg))
{
responseData.success = true;
responseData.message = "Resignation Submission form Created successfully";
return Json(responseData);
}
}
else
{
responseData.success = false;
var errors = ModelState.Select(x => x.Value.Errors)
.Where(y => y.Count > 0)
.ToList();
responseData.message = "Some Required Fields Not Added";
return Json(responseData);
}
}
else
{
responseData.success = false;
responseData.message = "No Data For This File No";
return Json(responseData);
}
return Json(responseData);
}
Jayraj ChhayaPosted Nov 17, 2023, 1:26 PM
Hello ahmed salah
Problem
The issue mentioned is that when the submit button is clicked multiple times for the same request, the SweetAlert message "Employee Exist Before" is displayed multiple times as well. The expected behavior is to display the message only once.
Cause
The cause of this issue is that the AJAX request is not properly handling the response and the SweetAlert message is being displayed multiple times in the success callback function.
Try these code