Hi @Ahmed Abd El Aziz,
In your action method, return Json(object) to return JSON to your page.
if (string.IsNullOrEmpty(resignationRequester.errorMsg))
{
resignationRequester.successMsg = "Resignation Submission form Created successfully";
return Json(new { success = true, responseText = resignationRequester.successMsg }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { success = false, responseText = resignationRequester.errorMsg }, JsonRequestBehavior.AllowGet);
}
Then get the responseText from the response in success.
success: function (response) {
if (response.success) {
Swal.fire({
icon: 'success',
title: 'Submition Request',
text: response.responseText
});
} else {
Swal.fire({
icon: 'error',
title: 'Submition Request',
text: response.responseText
});
}
},
Below is an example of my test, you can refer to it.
public class ResignationController : Controller
{
// GET: Resignation
public ActionResult RequesterIndex(string filenumber)
{
ResignationRequester resignationRequester = new ResignationRequester();
return View(resignationRequester);
}
[HttpPost]
public ActionResult RequesterIndex(FormCollection formCollection)
{
ResignationRequester resignationRequester = new ResignationRequester();
resignationRequester.DirectManager = Convert.ToInt32(formCollection["DirectManager"]);
resignationRequester.LineManager = formCollection["LineManager"];
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;
}
}
else
{
var errors = ModelState.Select(x => x.Value.Errors)
.Where(y => y.Count > 0)
.ToList();
resignationRequester.errorMsg = "Some Required Fields Not Added";
goto InvalidModel;
}
InvalidModel:
ViewBag.isPostBack = true;
if (string.IsNullOrEmpty(resignationRequester.errorMsg))
{
resignationRequester.successMsg = "Resignation Submission form Created successfully";
return Json(new { success = true, responseText = resignationRequester.successMsg }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { success = false, responseText = resignationRequester.errorMsg }, JsonRequestBehavior.AllowGet);
}
}
}
@{
ViewBag.Title = "Requester Index";
}
@using (Html.BeginForm("RequesterIndex", "Resignation", FormMethod.Post, new { enctype = "multipart/form-data", @id = "ResignationApp", style = "padding-top: 50px" }))
{
<div class="form-horizontal">
<div class="row">
<div class="form-group col-md-6 hover">
<div class="col-md-5">
@Html.LabelFor(model => model.DirectManager, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.DirectManager, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DirectManager, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group col-md-6 hover">
<div class="col-md-5">
@Html.LabelFor(model => model.LineManager, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.LineManager, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LineManager, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-0 col-md-12">
<input type="submit" value="Submit" class="btn btn-success" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.16/dist/sweetalert2.all.min.js"></script>
<script>
$("#ResignationApp").submit(function (e) {
e.preventDefault(); // Prevent the default form submission
var formData = $(this).serialize();
console.log("data is" + formData)
$.ajax({
type: "POST",
url: '@Url.Action("RequesterIndex", "Resignation")',
data: formData,
success: function (response) {
if (response.success) {
Swal.fire({
icon: 'success',
title: 'Submition Request',
text: response.responseText
});
} else {
Swal.fire({
icon: 'error',
title: 'Submition Request',
text: response.responseText
});
}
},
error: function (error) {
console.error(error);
}
});
});
</script>
public class ResignationRequester
{
[Required]
public int DirectManager { get; set; }
[Required]
public string LineManager { get; set; }
public string errorMsg { get; set; }
public string successMsg { get; set; }
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.