使用jquery绑定Kendo DataGrid

时间:2014-02-23 11:03:08

标签: jquery asp.net-mvc data-binding datagrid kendo-ui

我将信息从控制器传递到这样的视图:

return View(data);

如何在View中访问此数据以便数据绑定到KendoDataGrid? 我有以下代码:

@model IEnumerable<MyCustomerModel>
<div id="StatementGrid"></div>
<script>
    $(document).ready(function() {
        var lines = [{ TransactionDate: "01/01/2014", TransactionNumber: "001" }];//this works
        var lines2 = @model //How to pass the model information here?

        var grid = $("#StatementGrid").kendoGrid({
            columns: [
                { field: "TransactionDate", title: "Date" },
                { field: "TransactionNumber", title: "Transaction Number" }
            ],
            dataSource: {
                data:lines
                //data:lines2 //this does not work
            }
        });

    });
</script>

1 个答案:

答案 0 :(得分:0)

可以 Json对您的模型进行编码,并将您的JavaScript行变量设置为等于该值,如下所示:

@model IEnumerable<MyCustomerModel>


<div id="StatementGrid"></div>
<script>
    $(document).ready(function() {

        var lines = @Html.Raw(Json.Encode(Model))

        var grid = $("#StatementGrid").kendoGrid({
            columns: [
                { field: "TransactionDate", title: "Date" },
                { field: "TransactionNumber", title: "Transaction Number" }
            ],
            dataSource: {
                data:lines
            }
        });

    });
</script>

您必须设置日期数据类型和格式,但它会起作用。话虽这么说,我绝对不建议采用这种方法。相反,我会查看Telerik文档,了解如何使用以下链接使用远程或本地数据进行绑定,并遵循他们的指导:

http://demos.telerik.com/kendo-ui/web/grid/remote-data.html

http://demos.telerik.com/kendo-ui/web/grid/local-data.html