通过从控制器调用显示 toast 消息

Show toast message by calling from controller

我正在使用 MVC,我想根据来自控制器的函数结果将消息显示为 toast。 我的代码落后一步。

这是我的观点:

 @(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 succesfully',
               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");

       }

这是我的控制器:

[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);
        }

我应该在此处更改什么以显示基于 TempData["CheckerResult"] 结果的消息?它总是落后一步。我在逻辑上得到了这个问题,但不知道如何解决。

如有任何回复,我们将不胜感激。干杯

您正在检查首次呈现视图时存在的值:

var a = '@TempData["CheckerResult"]';

但在 AJAX 调用中未使用返回结果中的任何内容:

result

查看浏览器中的页面源代码,观察 @TempData["CheckerResult"] 本质上是如何变成 JavaScript 代码的硬编码文字值的。它不会改变。

基本上,当您不返回视图时,不要使用 TempData。您正在返回 JSON,其中包含您想要的信息:

return Json(new { success = true, responseText = "Succes" }, JsonRequestBehavior.AllowGet);

因此请检查您的 AJAX 响应处理程序中返回的信息:

if (result.responseText == "Succes")

您还需要更改此设置:

dataType: "html"

对此:

dataType: "json"

因为您期待从服务器返回 JSON,而不是 HTML。