【问题标题】:Webgrid paging through generic controller method.通过通用控制器方法进行 Webgrid 分页。
【发布时间】:2015-11-13 01:15:45
【问题描述】:

我有一个通用方法来获取应用程序中网格的数据,该方法从 API 获取数据,然后返回填充有所述数据的预定义网格。我的问题是,当我使用此方法时,分页将不起作用 - 即没有任何反应。

我认为这可能取决于分页链接正在调用的 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);

网络网格

@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>

【问题讨论】:

    标签: c# ajax asp.net-mvc-4 razor webgrid


    【解决方案1】:

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

    我只需要捕获事件并将?page=[pageNo] 附加到URL 并将发布数据包含在ajax 调用的data: 属性中。

    【讨论】:

    • 谢谢,这就是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2015-09-10
    • 2021-05-17
    相关资源
    最近更新 更多