I am currently utilizing MVC and I am looking to display a message as a toast based on the result of my function from the controller. However, my code seems to be functioning one step behind.
Below is My View:
@(Html.DevExtreme()
.Button()
.Icon("check")
.Hint("Check the User")
.OnClick("function(e) {CheckUser(e,data,rowIndex) }")
)
function CheckUser(e, f, g) {
console.log(e)
console.log(f.InsertedUserId)
console.log(f.AdminUserId)
$.ajax({
type: "POST",
url: '@Url.Action("CheckUser","UserRoleManagement")',
data: " {AdminUserId: '" + f.AdminUserId + "', InsertedUserId:'" + f.InsertedUserId + "', IsCSOUser: 'False'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (result) {
var a = '@TempData["CheckerResult"]';
if (a.toString() == "Succes") {
showToast(e)
}
else {
showToast3(e)
}
console.log(result);
}
})
};
function showToast(e) {
console.log('showToast');
$("#toast").dxToast({
message: 'Updated successfully',
type: 'success',
displayTime: 3000,
maxWidth: 500,
height: "auto",
animation: { show: { type: 'fade', duration: 400, from: 0, to: 1 }, hide: { type: 'fade', duration: 400, to: 0 } },
position: { my: 'center bottom', at: 'center bottom', of: window }
});
$("#toast").dxToast("show");
}
function showToast3(e) {
console.log('showToast');
$("#toast").dxToast({
message: 'Process Failed.',
type: 'error',
displayTime: 3000,
maxWidth: 500,
height: "auto",
animation: { show: { type: 'fade', duration: 400, from: 0, to: 1 }, hide: { type: 'fade', duration: 400, to: 0 } },
position: { my: 'center bottom', at: 'center bottom', of: window }
});
$("#toast").dxToast("show");
}
Here is my Controller:
[HttpPost]
public ActionResult CheckUser(string AdminUserId, string InsertedUserId, bool IsCSOUser)
{
RoleGroupRepository rep = new RoleGroupRepository();
//TempData["Success"] = "User is checked Successfully.";
SiteSession session = (SiteSession)SessionManager.ReturnSessionObject(SessionKeys.MainSession);
long CurrentLoggedUserId = session.AdminUserId;
if (CurrentLoggedUserId == Convert.ToInt64(InsertedUserId))
{
TempData["CheckerResult"] = "User check is not Successful.";
pLogger.INF("User check is not Successful. User can not check by the Inserted User.");
return Json(new { success = false, responseText = "Fail! User is not checked!" }, JsonRequestBehavior.AllowGet);
}
ReturnValuesParser returnValues = rep.CheckUser(AdminUserId, Convert.ToString(CurrentLoggedUserId), IsCSOUser);
if (returnValues.ReturnCode == 1)
{
TempData["CheckerResult"] = "Succes";
return Json(new { success = true, responseText = "Succes" }, JsonRequestBehavior.AllowGet);
}
else
{
TempData["CheckerResult"] = "User check is not Successful.";
pLogger.INF("User check is not Successful" + returnValues.returnDescription_En);
}
return Json(new { success = false, responseText = "Fail! User is not checked!" }, JsonRequestBehavior.AllowGet);
}
How can I adjust this to display my message based on the TempData["CheckerResult"] result correctly? It appears to always be referencing one step behind. While I understand the issue logically, I'm unsure how to resolve it.
I would appreciate any guidance or insights. Thank you!