我正在使用实体框架开发一个 Asp.net MVC3 应用程序。我使用 Knockoutjs 进行绑定,使用 KendoUI 作为视图的 UI 部分。我能够实现大部分 KendoUi 小部件,但现在我需要使用从 SQL 服务器获取数据的 KendoUI 网格控件。据我了解,网格小部件适用于 XML 或 JSON。
所以我有一个数据库上下文:
public DbSet<FranchiseInfoDto> Franchises { get; set; }
我已经在本地 Sql 服务器的表中保存了一些数据,并从控制器中检索它并将其序列化为 Json,以便我可以以某种方式将其绑定到视图中的网格widjet:
private OmegaDB db = new OmegaDB();
//
// GET: /Franchise/
public JsonNetResult Index()
{
JsonNetResult jsonNetResult = new JsonNetResult { Formatting = Formatting.Indented };
var franchises = db.Franchises.ToList();
jsonNetResult.Data = franchises;
return jsonNetResult;
}
序列化的 json 数据格式如下:
[{ ParentId: 0, Title: "Deposit", Type: "link", Link: "http://www.abv.bg" }, { ParentId: 2, Title: "Cash", Type: "link", Link: "http://www.facebook.com"}];
我阅读了有关 KendoUI Grid 的文档,并能够将其绑定到一些本地数据,如下所示:
var menus = [{ ParentId: 0, Title: "Deposit", Type: "link", Link: "http://www.abv.bg" }, { ParentId: 2, Title: "Cash", Type: "link", Link: "http://www.facebook.com"}];
var dataSource = new kendo.data.DataSource({
data: menus,
schema: {
model: {
fields: {
ParentId: { editable: true },
Title: { editable: true },
Type: { editable: true },
Link: { editable: true }
}
}
}
});
$("#grid").kendoGrid({
toolbar: ["create", "save", "cancel"],
columns: [
{
field: "ParentId",
title: "Id",
width: 50
},
{
field: "Title",
title: "Label",
width: 100
},
{
field: "Type",
title: "Type",
width: 100
},
{
field: "Link",
title: "Link"
}
],
dataSource: dataSource,
editable: true,
groupable: true,
scrollable: true,
sortable: true,
pageable: true
});
但是当我试图将它绑定到返回 Json 的 Index 控制器时,我没有成功。我试过这样的事情:
dataSource: {
type: "odata",
transport: {
read: "Franchise/Index" // this is my controller action //where I serialize the data coming from the local sql server to json
}
我对编程比较陌生,我不确定这种方法是否正确。任何基于我的示例代码的示例建议将不胜感激。谢谢你!