当用户点击MVC5中的浏览器后退按钮时,在检索数据时面临问题



当用户提交数据并检索包含超链接的html数据时,我正在处理页面。现在,当用户点击链接时,他将被导航到另一个页面,当用户单击浏览器后退按钮时,我想显示之前显示的html数据。为了实现这一点,我尝试将值存储在会话中,并在用户单击返回浏览器按钮时检索它。

但我面临问题

  1. 我试图捕获浏览器返回按钮并显示会话存储的变量,但我不确定为什么没有触发。

    $(window(.on('hashchange',function(({$("#spanId"(.html("test"(;});

  2. 存储在会话变量中的Html数据没有正确显示<quot;显示为"&lt;并且所有数据都显示为字符串而不是html内容。

    $(document).ready(function () {
    $("#spanId").html("@Session["Data"]");
    $("#btnSubmit").click(function () {
    var data = {
    "Date": $.datepicker.formatDate("mm-dd-yy", DateVal),
    "Type": $('#type').val(),
    }
    $.ajax({
    type: 'POST',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(data),
    url: '@Url.Action("GetReportdata", "Reports")',
    success: function (result) {
    $("#spanId").html(content);
    },
    error: function (xhr, ajaxOptions, thrownError) {
    }
    });
    });
    });
    Please let me know if there is any other way to handle this and help me resolves this issue.
    

在服务器端可能有另一种解决方法,但这应该有效。

$(document).ready(function () {
$("#spanId").html(decodeHtml("@Session["Data"]"));
$("#btnSubmit").click(function () {
var data = {
"Date": $.datepicker.formatDate("mm-dd-yy", DateVal),
"Type": $('#type').val(),
}
$.ajax({
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
url: '@Url.Action("GetReportdata", "Reports")',
success: function (result) {
$("#spanId").html(decodeHtml(content));
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
});
});
function decodeHtml(encodedHtml) {
return encodedHtml.replace(/&amp;/g,'&')
.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

我发现了一个处理浏览器返回按钮的事件:

if (window.performance && window.performance.navigation.type == 
window.performance.navigation.TYPE_BACK_FORWARD) {

$("#spanId").html("@Session["Data"]");
}

这可用于重新填充数据。

最新更新