首页 > 解决方案 > 重定向到 ASP .NET MVC 中的页面后显示通知

问题描述

下面是我的ajax代码:

$.ajax({
         url: '@Url.Action("AddUser", "ControllerName")',
         dataType: 'json',
         contentType: 'application/x-www-form-urlencoded; charset=utf-8',
         type: 'POST',
         data: {
                //data
         },
         success: function (data, textStatus, jqXHR) {
               console.log(jqXHR.status);
               if (data.isSuccess) {
                   alert(data.message);
               }
               else {
                   alert(data.message);
               }
               window.location.href = "@Url.Action("Index", "ControllerName")";
         },
         error: function (jqXHR, textStatus, errorThrown) {
               console.log(jqXHR.status);
               window.location.href = "@Url.Action("Index", "ControllerName")";

         }
});

而不是显示alert(data.message);我想在页面加载后显示自定义通知。

我不想传递任何查询字符串参数,因为它在 url 中可见。

有两种可能的方式来显示通知:

1)

$(".notificationdiv").html("<div class='notification  alert alert-success' role='alert'>
<strong> Operation performed successfully</strong>
</div>");
$(".notification").fadeOut(4000);
  1. 我有一个自定义的基本控制器虚拟方法来显示通知。在页面重定向后调用该方法。

请让我知道如何显示通知。代码示例表示赞赏。提前谢谢你。

标签: javascriptajaxasp.net-mvcwindow.location

解决方案


采用sessionStorage

在 window.location.href 之前的 ajax 成功添加这个

sessionStorage.successMessage= true;

并将其添加到您的 jquery 中

$(function () {
    if (sessionStorage.successMessage) {
        $(".notificationdiv").html("<div class='notification  alert alert-success' role='alert'><strong> Operation performed successfully</strong></div>");
        $(".notification").fadeOut(4000);
        sessionStorage.successMessage= false;
        sessionStorage.removeItem("successMessage") //if you want remove from session storage
    }
});

推荐阅读