Webgrid 通过通用控制器方法进行分页。

Webgrid paging through generic controller method.

我有一个通用方法来为我的应用程序中的网格获取数据,此方法从 API 获取数据,然后 returns 填充有所述数据的预定义网格。我的问题是,当我使用此方法时,寻呼将不起作用 - 即什么也没有发生。

我认为这可能是由于 URL 寻呼 link 正在调用 - 即通用方法 - 不知道需要发送其他数据。

如何让我的 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>

我在这里找到了答案:WebGrid pagination loose parameters with POST data

我只需要捕获事件并将 ?page=[pageNo] 附加到 URL 并将 post 数据包含在 data: 属性 中 ajax打电话。