我从事 asp.net MVC 应用程序。我遇到问题,当调用操作
RequesterIndex
控制器辞职时,无法显示错误消息或成功消息。
使用ajax请求调用API工作成功只需在甜蜜警报上显示错误消息或成功消息。
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;
}
预期结果
显示
resignationRequester.errorMsg
ON API AJAX 调用(如果存在)的消息
或
显示
resignationRequester.successMsg
ON API AJAX 调用(如果存在)的消息
含义显示行动结果
RequesterIndex
调用甜蜜警报
$('#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);
}
});
});
});
error: function(xhr, status, message) {
if (xhr.responseJSON.errors != []) {
for (const key in xhr.responseJSON.errors) {
const message = xhr.responseJSON.errors[key];
toastr.error(message);
}
}
}
尝试使用这个。