我第一次尝试使用Kendo网格:这是网格的代码:
@model IEnumerable<SustIMS.Models.ModelTest>
<div id="grid">
@(Html.Kendo().Grid(Model)
.Name("datagrid")
.Columns(columns =>
{
columns.Bound(c => c.Genre).Width(140);
columns.Bound(c => c.Title).Width(190);
columns.Bound(c => c.ReleaseDate);
columns.Bound(c => c.Price).Width(110);
})
.HtmlAttributes(new { style = "height: 90%;" })
.Scrollable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Show", "MasterData"))
)
)
</div>
这将显示没有任何数据的网格。我已经创建了一个测试模型,并尝试在控制器中添加这样的数据:
public ActionResult Show()
{
var list = new List<ModelTest>();
list.Add(new ModelTest { Title = "A Title", Price = 10.5m, ReleaseDate = DateTime.Now, ID = 1, Genre = "zz" });
list.Add(new ModelTest { Title = "B Title", Price = 11.5m, ReleaseDate = DateTime.Now, ID = 2, Genre = "zzzzzz" });
return Json(list);
}
这不显示网格中的任何数据。我怎样才能将数据传递给它?它是否必须来自模型,还是可以自定义方式添加?
答案 0 :(得分:4)
试试这个:
public ActionResult Show([DataSourceRequest] DataSourceRequest request)
{
var list = new List<ModelTest>();
list.Add(new ModelTest { Title = "A Title", Price = 10.5m, ReleaseDate = DateTime.Now, ID = 1, Genre = "zz" });
list.Add(new ModelTest { Title = "B Title", Price = 11.5m, ReleaseDate = DateTime.Now, ID = 2, Genre = "zzzzzz" });
return Json(list.ToDataSourceResult(request));
}
然后把它放在你的控制器中:
using Kendo.Mvc.Extensions;