如何在页面刷新时传递字符串?`

时间:2015-10-06 09:09:50

标签: c# .net asp.net-mvc razor

我遇到这样的问题。

情景:

在运行我的操作时,我使用int数组来检索基于这些ID的数据。这种方法非常有效,直到您到达视野。我设法检索数据很好,但每当我刷新页面时,由于数组是一个复杂类型,它在动作中以null形式出现。为了解决这个问题,我已将数组转换为字符串,并希望将此值传递给页面刷新时的操作。但到目前为止我所尝试的所有内容并没有实际提交值,参数肯定会被填充。我尝试传入的参数是RPString

动作:

[AuthorizeClientID]
        [LoggingFilter]
        public ActionResult SupplierReportSelection(int ClientID, int[] ReviewPeriodID, string RPString, int? GroupID, int? SupplierID = null, bool? Backbtn = null, int? StatusCategoryID = null) {
            if (TempData["TempReviewPeriod"] != null) {
                ReviewPeriodID = (int[])TempData["TempReviewPeriod"];
            }
            ClaimsBySupplierViewModel SupplierModel = ClaimsBySupplier(ClientID, ReviewPeriodID, SupplierID, GroupID);

            ViewBag.client = client;


            return View("ClaimsBySupplier", SupplierModel);
        }

查看:

 @if (Model.ReportData.Any()) {
            Model.RPString = string.Join(",", Model.inputReviewPeriodIDs);
            Model.RPString = Json.Encode(Model.RPString);          
     //Removed code which isn't needed
        }

如果JQuery也是可能的,请告诉我。

2 个答案:

答案 0 :(得分:0)

jQuery有unload方法,当用户离开页面时会调用该方法。

例如,您可以执行以下操作:

$( window ).unload(function() {
    $.ajax({
        method: "<method>",
        url: "<your url>",
        data: { RPString: <your desired value> }
    })
});

答案 1 :(得分:0)

   [AuthorizeClientID]
        [LoggingFilter]
        public ActionResult SupplierReportSelection(int ClientID, int[] ReviewPeriodID, string RPString, int? GroupID, int? SupplierID = null, bool? Backbtn = null, int? StatusCategoryID = null) {
            if (GroupID == null && SupplierID == null && TempData["TempReviewPeriod"] != null) {
                ReviewPeriodID = (int[])TempData["TempReviewPeriod"];

                //The cookie code is used for the page refresh whenever a user performs a search with multiple ReviewPeriod IDs. This is because you cannot pass the ReviewPeriodID array in the URL.
                Request.Cookies.Remove("RPCookie");
                HttpCookie RPCookie = new HttpCookie("RPCookie");
                for (int i = 0; i < ReviewPeriodID.Length; i++) {
                    if (ReviewPeriodID.Length - 1 != i) {
                        RPCookie.Value += ReviewPeriodID[i] + ",";
                    }
                    else {
                        RPCookie.Value += ReviewPeriodID[i];
                    }
                }
                Response.Cookies.Set(RPCookie);
            }
            else if (GroupID == null && SupplierID == null && TempData["TempReviewPeriod"] == null) {
               ReviewPeriodID = Request.Cookies.Get("RPCookie").Value.Split(',').Select(n => Convert.ToInt32(n)).ToArray();

            }

            ClaimsBySupplierViewModel SupplierModel = ClaimsBySupplier(ClientID, ReviewPeriodID, SupplierID, GroupID);

            ViewBag.client = client;

            return View("ClaimsBySupplier", SupplierModel);
        }

我从上面的Wiktor Zychla那里得到了一个建议,并使用cookie来存储数据。每次TempData中有数据时都会删除cookie并重新创建cookie。然后,如果TempData为空(它将在页面刷新时),我将获取cookie并将其转换为我的参数。