Asp.net mvc 如何在剑道UI网格上执行服务器端过滤

Asp.net mvc 如何在剑道UI网格上执行服务器端过滤,asp.net-mvc,kendo-ui,kendo-grid,asp.net-ajax,Asp.net Mvc,Kendo Ui,Kendo Grid,Asp.net Ajax,我正在尝试为剑道UI网格(仅限客户端)实现服务器端过滤。我不知道如何传递筛选器运算符和在筛选器框中输入的值。我能够实现服务器分页,并希望过滤与服务器分页一起工作,即显示第2页,共5条过滤行。我看到了一些将请求绑定到“DataSourceRequest”对象的示例,但我们没有服务器端KendoUI的许可证,只能通过客户端更改来实现 以下是我的jQuery代码: var page = 1; var pageSize = 5; var title = "test"; var selectWork =

我正在尝试为剑道UI网格(仅限客户端)实现服务器端过滤。我不知道如何传递筛选器运算符和在筛选器框中输入的值。我能够实现服务器分页,并希望过滤与服务器分页一起工作,即显示第2页,共5条过滤行。我看到了一些将请求绑定到“DataSourceRequest”对象的示例,但我们没有服务器端KendoUI的许可证,只能通过客户端更改来实现

以下是我的jQuery代码:

var page = 1;
var pageSize = 5;
var title = "test";

var selectWork = function (e) {
    alert("selected");
};

$("#selectWorkGrid").empty();
$("#selectWorkGrid").kendoGrid({
    dataSource:
    {
        transport: {
            read: {
                url: "http://example.com/" + "work/SearchWorkJ?worktitle=" + title,
                dataType: "json",
                contentType: "application/json",
                data: {
                    page: page,
                    pageSize: pageSize
                }
            },
            serverFiltering: true,                    
            parameterMap: function (data, type) {
                if (type == "read") {
                    return {
                        page: data.page,
                        pageSize: data.pageSize
                    }
                }
            }
        },
        schema: {
            model: {
                id: "workId",
                fields: {
                    workId: { type: "number" },
                    workTitle: { type: "string" },
                    writers: { type: "string" },
                    durationInMmSs: { type: "string" }
                }
            },
            data: "data",
            total: "total"
        },
        pageSize: pageSize,
        serverPaging: true,
        serverFiltering: true
    },
    sortable: true,
    resizable: true,
    columnMenu: false,
    filterable: {
        mode: "row",
        extra: false,
        operators: {
            string: {
                startswith: "Starts with",
                eq: "Is equal to",
                neq: "Is not equal to"
            }
        }
    },
    noRecords: {
        template: "No results available."
    },
    pageable: {
        numeric: false,
        refresh: true,
        buttonCount: 15
    },
    scrollable: false,
    columns: [
        {
            field: "workTitle",
            title: "Title",
            template: "#=workTitle#"
        },
        {
            field: "writers",
            title: "Writers",
            filterable: false,
            template: "${writers == null ? '':writers}",
            width: 300
        },
        {
            field: "durationInMmSs",
            title: "Duration",
            filterable: false,
            headerAttributes: { style: "text-align:right;" },
            attributes: { style: "text-align:right;" },
            width: 80
        },
        { command: { text: "Select", click: selectWork }, title: "", width: 60 }
    ]
});
返回json的控制器操作:

public ContentResult SearchWorkJ(string workTitle, int page = 0, int pageSize = 0)
{
    var worksJson = "";
    var works = WorkService.SearchWork(workTitle, page, pageSize);
    if (works != null)
    {
        // Set total to upto current record + 1 so that next button works in kendo
        int totalCount = page * pageSize + 1;
        var sortedWorks = new List<WorkViewModel>();
        sortedWorks.AddRange(works.Select(w => new WorkViewModel
        {
            WorkId = w.WorkId,
            WorkTitle = w.WorkTitle,
            Writers = w.Writers,
            DurationInMmSs = w.Duration
        }).OrderBy(w => w.WorkTitle));
        worksJson = JsonConvert.SerializeObject(new { total = totalCount, data = sortedWorks });
    }
    return new ContentResult { Content = worksJson, ContentType = "application/json" };
}
public ContentResult SearchWorkJ(字符串workTitle,int page=0,int pageSize=0)
{
var=”;
var works=WorkService.SearchWork(工作标题、页面、页面大小);
如果(有效!=null)
{
//将总数设置为当前记录+1,这样下一个按钮在剑道中起作用
int totalCount=页面*页面大小+1;
var sortedWorks=新列表();
sortedWorks.AddRange(works.Select(w=>new WorkViewModel
{
WorkId=w.WorkId,
WorkTitle=w.WorkTitle,
作家,
持续时间NMMSS=w.持续时间
}).OrderBy(w=>w.WorkTitle));
worksJson=JsonConvert.SerializeObject(新的{total=totalCount,data=sortedWorks});
}
返回新的ContentResult{Content=worksJson,ContentType=“application/json”};
}
如果你看这个

到您的服务器/api

现在,如果您有一个共享此结构的模型,您可以按以下格式响应

{
   "Data" : <your array of models>,
   "Total" : the number of models that fits your filter regardless of the filter, this helps kendo grid knowing how many pages there is for the pager.,
   "Errors" : is mostely used for create and update so just return null
}
另一种选择是使用


请阅读回复并告知我是否回答了您的问题,好吗?没问题,请欣赏:)
{"take":5,"skip":0,"page":1,"pageSize":5,"filter":{"filters":[{"field":"FirstName","operator":"eq","value":"David"}]}}
{
   "Data" : <your array of models>,
   "Total" : the number of models that fits your filter regardless of the filter, this helps kendo grid knowing how many pages there is for the pager.,
   "Errors" : is mostely used for create and update so just return null
}
[HttpGet][Route("FindClients")]
public IHttpActionResult FindClients(string filterField = null, string filterValue = null, 
string sortProperty = "Id", int? page = null, int pageSize = 50)
{
    var ctx = new MyDbContext();
    var query = ctx.Clients.AsQueryable();

    if (!string.IsNullOrEmpty(filterField) && !string.IsNullOrEmpty(filterValue))
    query = query.Query(t => t.Contains(filterField, filterValue)).OrderBy(sortProperty);

    //  count.
    var clientCount = query.Count();
    int? pages = null;

    if (page.HasValue && pageSize > 0)
    {
    if (clientCount == 0)
        pages = 0;
    else
        pages = clientCount / pageSize + (clientCount % pageSize != 0 ? 1 : 0);
    }

    if (page.HasValue)
    query = query.Skip((page.Value-1) * pageSize).Take(pageSize);

    var clients = query.ToList();

    return Ok(new
    {
    total = clientCount,
    pages = pages,
    data = clients
    });
}