0

我正在使用带有 asp.net MVC 的网格。目前,网格.DataSource(c => c.Read())用于调用一个 URL,该 URL 返回一个IEnumerable<item>进入网格的 URL。

我想在网格中实现分页。然而,数据没有正常的分页方法(计数、页码等)。

相反,当我在内部检索数据时,我会像这样取回数据:

{ items: [. . . ], nextPage: 'J23jeg9e93', previousPage: 'oqow0r93285' }

要获取下一页,您必须再次请求数据,但要包含下一页或上一页的分页令牌。

目前我只是将项目数组返回到网格,所以没有 nextPage/previousPage 元数据。

我看不到任何包含此元数据的方法,因为我只是返回一个 IEnumerable 项目,因此没有包装器对象可以放入元数据。

我可以使用.Data()将元数据附加到读取请求,但我需要反过来做。一旦我得到元数据,我需要能够将它存储在一个 javascript 变量中,以便我可以发送它.Data()

4

1 回答 1

1

我不知道您实际上是如何触发 NextPage、PreviousPage 操作的,但是...

您可以使用 MVC 自定义数据源配置来提供对更多选项(如 Schema 配置)的访问。 http://docs.telerik.com/aspnet-mvc/getting-started/custom-datasource

Schema 配置允许您添加一个 Parse 函数,该函数可以采用您的自定义结果格式:

{ items: [. . . ], nextPage: 'J23jeg9e93', previousPage: 'oqow0r93285' }

并提取项目(给网格)和下一个页面,上一个页面值(供您存储以传递给下一个读取请求)。

例如:

示例网格:

@(Html.Kendo().Grid<TelerikMvcApp4.Models.GridViewModel>()
.Name("grid")
.DataSource(ds => ds
    .Custom()
    .Batch(true)
    .Schema(schema => schema
        .Parse(@<text>parseData</text>)
    )
    .Transport(transport => transport
        .Read(read => read.Action("Grid_Read", "Home").Type(HttpVerbs.Post).Data("readData"))
    )
    .PageSize(1)
    .ServerPaging(true)
)
.Pageable()
)

示例 parseData 和 readData javascript:

<script>
var nextPage,
    previousPage;
function readData() {
    // Return the "extra" data that should be posted with each grid read request, which is the nextPage/previousPage we were given in the previous request respsonse.
    return {
        nextPage: nextPage,
        previousPage: previousPage
    };
}
function parseData(data) {
    // Parse the response from the server as it isn't in the typical format expected by the grid.

    // Extract your nextPage/previousPage, store it somewhere so they can be added to the next grid request.
    nextPage = data.nextPage;
    previousPage = data.previousPage;

    // Return the actual data that should be displayed in the grid.
    return data.items;
}
</script>

示例网格读取操作:

[HttpPost]
    public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request, string nextPage, string previousPage)
    {

        // "Fetch" the data, presumably doing something with nextPage and previousPage...
        var items = new List<GridViewModel>()
        {
            new GridViewModel() { name = "bob", age = 23},
            new GridViewModel() { name = "jim", age = 43},
        };
        // Determine what the new nextPage, previousPage should be...
        var newNextPage = "J23jeg9e93";
        var newPreviousPage = "oqow0r93285";

        return Json(new
        {
            items = items,
            nextPage = newNextPage,
            previousPage = newPreviousPage
        });
    }

这不是一个完整、强大的解决方案,但我认为它可以变得可行,并且至少会为您指明一个可能的方向。

于 2017-02-03T14:30:19.127 回答