Kendo排序不起作用

时间:2016-01-18 08:29:40

标签: c# jquery .net model-view-controller kendo-grid

我已经实现了如下所示的kendo网格,分页工作没有任何问题,但排序不起作用。你能帮忙吗

CSHTML

<div class="panel">
<div id="CsHistory" class="row">
    <div class="col-lg-12 col-md-12 col-sm-12" style="float:none; margin-left:auto; margin-right:auto; margin-top: 10px; margin-bottom: 10px;">
        @using PC.Cmgr.Claims.Domain.Models;
        @using PC.Cmgr.Claims.Domain.Common;
        @using PC.Cmgr.Models;
        @using System.Linq;
        @(Html.Kendo().Grid<CsAuditTrailViewModel>
            ()
            .HtmlAttributes(new { style = "width:auto; height:auto; text-center;margin-right: 30px;margin-left: 30px; " })
            .Name("AllCsHistory")
            .Columns(columns =>
            {
                columns.Bound(o => o.CsAuditTrailId).Title("CsAuditTrailId");                
            })
            .ToolBar(toolBar =>
            {
            })
            .Resizable(resize => resize.Columns(false))
            .Reorderable(reorder => reorder.Columns(true))
            .Sortable()
            .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
            .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .ServerOperation(true)
            .Model(model =>
            {
                model.Id(o => o.CsAuditTrailId);
            })
            .Read(read => read.Action("GetCHistoryByClaimId", "Claims", new { ClaimId = Model.Claim.ClaimId }))
            .Events(events => { events.Sync("sync_handler"); }
            )
            )
        )

    </div>

</div>

控制器

 public async Task<ActionResult> GetCsHistoryByClaimId([DataSourceRequest] DataSourceRequest request, Guid ClaimId)
    {
        var CsHistory = await _CsHistoryProxy.GetCsHistoryByClaimId(ClaimId);
        var rawData = new ConcurrentBag<CsAuditTrailViewModel>();
        var gridData = new List<CsAuditTrailViewModel>();
        Parallel.ForEach(CsHistory, (x) =>
        {
            rawData.Add(
                new CsAuditTrailViewModel
                {
                    CsAuditTrailId = x.CsAuditTrailId,                      
                    NewData = x.NewData,
                    OldData = x.OldData,                       
                    UpdateDate = x.UpdateDate,
                    UserId = x.UserId                                        
                });
        });



        ViewData["total"] = rawData.Count();

        // Apply paging
        if (request.Page > 0)
        {             
            gridData = rawData.Skip((request.Page - 1) * request.PageSize).ToList();
        }

        gridData = gridData.Take(request.PageSize).ToList();


        var result = new DataSourceResult()
        {
            Data = gridData,
            Total = (int)ViewData["total"]
        };

        return Json(result);
    }

2 个答案:

答案 0 :(得分:1)

您必须根据您的解决方案进行自定义。 排序字段位于DataSourceRequest中,您可以在控制器中处理它们。见这个例子:

namespace eval temp {
    proc silentEval {script} {
        rename ::puts ::original_puts
        proc ::puts args {}
        catch [list uplevel 1 $script] msg opts
        rename ::puts {}
        rename ::original_puts ::puts
        return -options $opts $msg
    }
}

您必须在分页之前进行排序......

取自:http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/custom-binding

答案 1 :(得分:1)

来自Telerik常见问题解答,

  

对于ajax绑定方案,必须使用ToDataSourceResult扩展方法来执行数据处理

所以,你必须这样做,

var result = new DataSourceResult()
{
    Data = gridData.ToDataSourceResult(request).Data,
    Total = (int)ViewData["total"]
};