我无法将数据从Web API控制器绑定到Kendo UI网格。不幸的是,我还没有找到任何这方面的例子。如何将Kendo UI网格绑定到Web API控制器?
这里的API控制器:
public class FruitController : ApiController
{
public class Fruit
{
public string Name { get; set; }
public string Color { get; set; }
}
public IEnumerable<Fruit> GetFruits()
{
List<Fruit> list = new List<Fruit>();
Fruit f = new Fruit();
f.Name = "Apple";
f.Color = "Red";
list.Add(f);
f = new Fruit();
f.Name = "Kiwi";
f.Color = "Green";
list.Add(f);
return list;
}
}
而在我的.cshtml文件我有:
@model IEnumerable<FruitController.Fruit>
@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
columns.Bound(p => p.Color);
})
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetFruits", "api/Fruit").Type(HttpVerbs.Get)
)
)
)
当我运行此,我从控制器成功的JSON响应:
[{“Name”:“Apple”,“Color”:“Red”},{“Name”:“Kiwi”,“Color”:“Green”}]
但是,没有数据。有什么明显的我失踪?我一直无法弄清楚这一点!
谢谢!
谢谢,将我的APIController方法的返回类型更改为DataSourceResult允许Grid显示数据。但是,如果我尝试使用现在失败的自动完成控件。那么这个网格控件不能用于标准的APIController是否正确?当其他客户端使用它们时,必须将所有APIController结果设置为DataSourceResult类型似乎是错误的。 – Dave 2012-07-23 19:11:31