2015-08-19 55 views
0

我有一個通用的方法來獲取我的應用程序中的網格數據,這種方法從API獲取數據,然後返回填充了所述數據的預定義網格。我的問題是,當我使用這種方法時,分頁將不起作用 - 即沒有任何反應。通過通用控制器方法的Webgrid分頁。

我認爲這可能是由於分頁鏈接正在調用的URL - 即通用方法 - 而不知道需要發送其他數據。

我怎樣才能讓我的webgrid的分頁工作與這種通用的方法?

泛型方法

/// <summary>Generic grid request method. Used to render any grid with data from a defined location.</summary> 
    /// <param name="obj">The data required to complete the operation.</param> 
    /// <param name="obj[dataurl]">The URL to be used to get the data for the grid.</param> 
    /// <param name="obj[data]">Any data that needs to be sent to the data source when getting the data for the grid.</param> 
    /// <param name="obj[gridurl]">The URL for the grid to be rendered.</param> 
    /// <returns>The grid populated with data in HTML format.</returns> 
    [HandleError] 
    [HttpPost] 
    public ActionResult GridRequest(string obj) 
    {    
     JObject Request; 
     string DataUrl, GridUrl, RequestData; 
     try 
     { 
      Request = JObject.Parse(obj); 
      DataUrl = (string)Request["dataurl"]; 
      RequestData = Request["data"] != null ? Request["data"].ToString() : null; 
      GridUrl = (string)Request["gridurl"]; 

      HttpClient client = new HttpClient(); 
      client.Request.ForceBasicAuth = true; 
      client.Request.SetBasicAuthentication(C4SMVCApp.Properties.Settings.Default.APIUsername, C4SMVCApp.Properties.Settings.Default.APIPassword); 
      HttpResponse response = client.Post(DataUrl, RequestData, HttpContentTypes.ApplicationJson); 
      string result = response.RawText; 

      if (response.StatusCode != HttpStatusCode.OK) 
      { 
       throw new Exception("Grid Request Error" + result); 
      } 
      else 
      { 
       JArray data = JArray.Parse(result); 
       return PartialView(GridUrl, data); 
      } 
     } 
     catch (Exception) 
     {     
      throw; 
     } 
    } 

Ajax調用

  $.ajax({ 
       type: "post", 
       url: "/C4S/Home/GridRequest", 
       data: { 
        obj: JSON.stringify({ 
         dataurl: "{0}Community/AANewsApi/AddToList".format(apiBaseUrl), 
         data: new Object({ listId: listId, items: selectedResult }), 
         gridurl: "~/Areas/Comms/Views/Home/Grids/ListPeopleGrid.cshtml" 
        }) 
       } 
      }).done(function (data) { 
       $('#persongrid').replaceWith(data); 
       $('#addusercontainer').addClass('hidden'); 
       listGrid(); 
      }).fail(failCallback); 

的WebGrid

@model IEnumerable<object> 
@{ 
    WebGrid ListPeopleGrid = new WebGrid(
    source: Model, 
    ajaxUpdateContainerId: "persongrid", 
    canPage: true, 
    rowsPerPage: 16); 
} 
<div id="persongrid"> 
@ListPeopleGrid.GetHtml(
    tableStyle: "webgrid", 
    headerStyle: "webgrid-header color-2", 
    footerStyle: "webgrid-footer color-2", 
    rowStyle: "webgrid-row", 
    alternatingRowStyle: "webgrid-row", 
    fillEmptyRows: true, 
    nextText: "Next >", 
    previousText: "< Prev", 
    columns: ListPeopleGrid.Columns(
    ListPeopleGrid.Column("Id", style: "hidden"), 
    ListPeopleGrid.Column("Name"), 
    ListPeopleGrid.Column("Manager"), 
    ListPeopleGrid.Column("AccessLevel"), 
    ListPeopleGrid.Column("Site"), 
    ListPeopleGrid.Column("Department") 
)) 
</div> 

回答

0

我發現這個問題的答案^ h ere:WebGrid pagination loose parameters with POST data

我只需要捕獲事件並將?page=[pageNo]添加到URL中,並將發佈數據包含在ajax調用的data:屬性中。

+0

謝謝,這是我一直在尋找。 –