Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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

What I have tried:

$("#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');
             
                }
            });
    });
 [HttpPost]
  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);
     
  }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900