ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 65.2k

How to display success message or error message when make jquery

Sep 13 2023 8:53 AM

I work on asp.net MVC application . I face issue I can't display error message or success message when call

action RequesterIndex controller Resignation .

calling API using ajax request working success only need to display error message or success message on sweet alert .

public async Task RequesterIndex(ResignationRequester resignationRequester) {
  var filenumber = resignationRequester.EmpID;
  if (Session[SessionKeys.UserCode] != null) {
    if (ModelState.IsValid) {
      if (resignationRequester.DirectManager == 0) {
        resignationRequester.errorMsg = "Department Manager Must Be Bigger Than 0";
        goto InvalidModel;
      }
      if (Convert.ToString(resignationRequester.LineManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.LineManager.ToString())) {
        resignationRequester.errorMsg = "Length Line Manager Must Be equal 6 or More";
        goto InvalidModel;
      }
      if (Convert.ToString(resignationRequester.DirectManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.DirectManager.ToString())) {
        resignationRequester.errorMsg = "Length Direct Manager Must Be equal 6 or More";
        goto InvalidModel;
      }
      if (!string.IsNullOrEmpty(Convert.ToString(resignationRequester.LineManager)) && resignationRequester.LineManagerName == null) {
        resignationRequester.errorMsg = "Line Manager Name Blank";
        goto InvalidModel;
      }
      if (julianRequestDate > 0 && SubmitionDate > 0 && julianRequestDate < SubmitionDate) {
        resignationRequester.errorMsg = "Last Worked Date Must be Bigger than Submit Date";
        goto InvalidModel;
      }

      int checkEmployeeNoExist = jde.CheckEmployeeExistOrNot(resignationRequester.EmpID);
      if (checkEmployeeNoExist >= 1) {
        resignationRequester.errorMsg = "Employee Exist Before";
        goto InvalidModel;
      }
     try {
        Workforce.InsertToReignation(resignationRequester, (string) Session[SessionKeys.Username], (DateTime) Session[SessionKeys.LastWorkingDate], noticeperiod, (int) Session[SessionKeys.UserCode]);
      } catch (Exception ex) {
        resignationRequester.errorMsg = "Create Not Done Correctly";
      }

      if (string.IsNullOrEmpty(ViewBag.errorMsg)) {
        resignationRequester.successMsg = "Resignation Submission form Created successfully";
      }
    } else {
      var errors = ModelState.Select(x => x.Value.Errors)
        .Where(y => y.Count > 0)
        .ToList();
      resignationRequester.errorMsg = "Some Required Fields Not Added";
      goto InvalidModel;
    }
  } else {
    resignationRequester.errorMsg = "No Data For This File No";
  }
  InvalidModel:
    ViewBag.isPostBack = true;
}

Expected result

display message of resignationRequester.errorMsg ON API AJAX Call if it exist

OR

display message of resignationRequester.successMsg ON API AJAX Call if it exist

meaning display result of action RequesterIndex calling on sweet alert

$('#btnsubmit').click(function() {
  $("#ResignationApp").submit(function(e) {
    e.preventDefault(); // Prevent the default form submission
    // Serialize the form data
    var formData = $(this).serialize();
    console.log("data is" + formData)
    $.ajax({
      type: "POST",
      url: '@Url.Action("RequesterIndex", "Resignation")',
      data: formData,
      success: function(response) {
        var errorMsg = '@Html.Raw(Json.Encode(ViewData["ErrorMessage"]))';
        Swal.fire({
          icon: 'error',
          title: 'Submition Request',
          text: errorMsg
        });
      },
      error: function(error) {
        // Handle any errors here
        console.error(error);
      }
    });
  });
});

 


Answers (2)