2015-10-06 81 views
2

我遇到了这样的问题。如何在页面刷新时传递一个字符串?`

场景:

当运行我的行为我用一个int数组来检索基于这些ID的数据。这完美的作品,直到你到达视图。我设法检索数据的罚款,但每当我刷新页面,因为数组是一个复杂的类型,它通过作为空行动。为了解决这个问题,我将数组转换为字符串,并希望将此值传递给页面刷新上的操作。但是到目前为止我尝试过的所有内容都没有提交价值,这个参数肯定会被填充。这我想传递的参数是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是也是一种可能性,只是让我知道。

+0

在第一次成功请求期间数组是如何填充的? –

+0

@WiktorZychla该数组填充了一个下拉列表,这取决于用户单击的值 –

+0

不完全是我的意思。我的问题是:传递给GET请求的查询字符串的列表,还是这是一个POST的列表值?无论如何,你有没有想过只是将它存储在Session中,以便当它为空时,从Session中检索它? –

回答

0

jQuery有unload方法,当用户正在离开页面时被调用。

例如,你可以做到以下几点:

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

我是否需要将参数传递给URL,然后尝试在动作中检索它? –

+0

@AndrewKilburn,在这个'unload'回调函数中,你可以向[<主机和控制器名称>/SupplierReportSelection']发送[ajax](http://api.jquery.com/jquery.ajax/)请求并传递你的'RPString '参数。 (如果我正确理解你的问题当然) – ieaglle

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。

相关问题