服务器端操作仅将数据返回到特定页面的网格

时间:2014-12-12 11:11:52

标签: c# linq kendo-ui kendo-grid kendo-asp.net-mvc

我使用telrik mvc grid我可以让分页工作,但想知道如何只返回用户可以查看的数据。

例如,用户总共有50条记录,页面大小设置为5,当前页面为1。 在初始加载时我只想返回前5行。然后,如果用户点击第二页,则接下来的5等。

我可以看到DataSourceRequest请求具有页面和页面大小等属性。

我是否配置此客户端或通过控制器?

我是否从请求页面和页面大小中获取值并将其传递给我的实体,然后执行linq查询或是否有更简单的解决方案?

有什么想法吗?感谢

@model IEnumerable<TelerikChecklist.Models.ProductViewModel>

@(Html.Kendo().Grid(Model)
.Name("gridPaging")
.Columns(columns =>
{
    columns.Bound(p => p.ProductName);
    columns.Bound(p => p.UnitsInStock);
    columns.Bound(p => p.UnitPrice);

})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:250px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .PageSize(5)
    .ServerOperation(true)
    .Events(events => events.Error("errorHandler"))
    .Model(model =>
    {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).Editable(false);
        model.Field(p => p.CategoryID).DefaultValue(1);
    })

    .Read(read => read.Action("ServerPaging_Read", "Home"))
)
)

<script type="text/javascript">


    function errorHandler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }




</script>

1 个答案:

答案 0 :(得分:0)

控制器需要以下控制器代码:

 public ActionResult CustomAjaxBinding_Read([DataSourceRequest] DataSourceRequest request)
    {
        var dataContext = new SampleEntities();

        // Convert to view model to avoid JSON serialization problems due to circular references.
        IQueryable<OrderViewModel> orders = dataContext.Orders.Select(o => new OrderViewModel
        {
            OrderID = o.OrderID,
            ShipCity = o.ShipCity,
            ShipCountry = o.ShipCountry,
            ShipName = o.ShipName
        });

        orders = orders.ApplyOrdersFiltering(request.Filters);

        var total = orders.Count();

        orders = orders.ApplyOrdersSorting(request.Groups, request.Sorts);

        if (!request.Sorts.Any())
        {
            // Entity Framework doesn't support paging on unsorted data.
            orders = orders.OrderBy(o => o.OrderID);
        }

        orders = orders.ApplyOrdersPaging(request.Page, request.PageSize);

        IEnumerable data = orders.ApplyOrdersGrouping(request.Groups);

        var result = new DataSourceResult()
        {
            Data = data,
            Total = total
        };

        return Json(result);
    }

观点:

@(Html.Kendo().Grid<TelerikChecklist.Models.Order>()
.Name("Grid")
.Columns(columns =>
{
    columns.Bound(o => o.OrderID).Groupable(false);
    columns.Bound(o => o.ShipCity);
    columns.Bound(o => o.ShipCountry);
    columns.Bound(o => o.ShipName);
})
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
.Groupable()
.DataSource(dataSource => dataSource
    .Ajax()
    .Read(read => read.Action("CustomAjaxBinding_Read", "Home"))
)
)