我的Kendo网格没有填充从Controller返回的JSON对象

时间:2015-12-10 15:22:32

标签: asp.net-mvc kendo-ui kendo-grid

我正在编写一个MVC应用程序,它将某些信息存储在会话变量中。我能够在我的repo类中填充一个没有问题的列表。我遇到的问题是,当我点击我的搜索客户端控制器时,它只是给了我一个JSON对象,但没有填充我的剑道网格。

这是我的数据源:

var clientSearch = new kendo.data.DataSource({
    transport: {
        read: {
            url: "SearchClient",
            contentType: "application/json; charset=utf-8",
            dataType: "json"    
        },

        create: {
            url: "ClientInformation",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            type: "POST"
        },
        parameterMap: function (data, operator) {
            if (operator != "read")
                return JSON.stringify(viewModel);
        }
    },
    schema: {
        model: {
            id: "clientName", 
        }
    }
});

这是我的网格:

$("#grid").kendoGrid({
    dataSource: clientSearch,
    columns: [{
        field: "clientName",
        title: "Client Name",            
    },
    {
        field: "clientNumber",
        title: "Client Number",
    },
    {
        field: "clientType",
        title: "Client Type",
    }]        
})

这是我的控制器,它返回我的JSON对象:

[HttpGet]
public ActionResult SearchClient()
{
    HttpSessionStateBase session = HttpContext.Session;
    Repo repo = new Repo(session);
    var result = repo.GetClient();

    return Json(new
    {
        list = result,
        count = result.Count
    }, JsonRequestBehavior.AllowGet);
}

2 个答案:

答案 0 :(得分:0)

您需要在数据源对象的架构中指定字段:

var clientSearch = new kendo.data.DataSource({
transport: {
    read: {
        url: "SearchClient",
        contentType: "application/json; charset=utf-8",
        dataType: "json"    
    },

    create: {
        url: "ClientInformation",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST"
    },
    parameterMap: function (data, operator) {
        if (operator != "read")
            return JSON.stringify(viewModel);
    }
},
schema: {
    model: {
        id: "clientName", 
        fields: {
            ID: { editable: false },
            clientName: { editable: false },
            clientNumber: { editable: false },
            clientType: { editable: false }
        }
    }
});

答案 1 :(得分:0)

Kendo.Mvc在绑定网格时需要DataSourceRequest and DataSourceResult

public ActionResult SearchClient([DataSourceRequest] DataSourceRequest request)
    {
        HttpSessionStateBase session = HttpContext.Session;
        Repo repo = new Repo(session);
        var result = repo.GetClient();

        return Json(new
        {
            list = result.ToList().ToDataSourceResult(request, ModelState)
        }, "application/json", JsonRequestBehavior.AllowGet);           
    }