我正在尝试让 Kendo UI 网格 MVC 在 .net core 中工作,从 PostgreSQL 数据库读取数据。
我建立了一个项目,将其连接到数据库,使用适当的 View 搭建 Controller ,并且它工作正常(意味着 CRUD 操作工作正常)。 现在我想把它和剑道联系起来。 我遵循 kendo website 的指南并安装成功(尽管它没有在 nugget 中提供专业版,所以我必须安装试用版??)。我在 _Layout.cshtml 中添加了所有这些 CSS/js 文件
这是我在 Controller 中的代码:
public ActionResult kendo()
{
return View();
}
// GET for Kendo
public ActionResult Categories_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(GetCategories().ToDataSourceResult(request));
}
public static IEnumerable<Category> GetCategories()
{
var result = from c in _context.Categories
select new Category()
{
Id = c.Id,
Name = c.Name
};
return result;
}
这是我在 kendo.cshtml 中的代码
@(Html.Kendo().Grid<Category>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.Id).Title("id");
columns.Bound(c => c.Name);
})
.HtmlAttributes(new { style = "height: 550px;" })
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Categories_Read", "Categories"))
.PageSize(20)
)
模型很简单
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
问题是剑道网格中没有显示数据。我可以在调试器中看到有向前的请求和向后的数据,但网格中没有显示任何内容。
?
最佳答案
该问题很可能是由 new ASP.NET Core serialization mechanism 引起的。请遵循此处的“第二个”第 4 步中的准则:
http://docs.telerik.com/kendo-ui/aspnet-mvc/mvc-core/getting-started
Step 4 Open Startup.cs, using a text editor (IDE) and update it as described below.
Locate the ConfigureServices method and add a call to services.AddKendo at the end.
public void ConfigureServices(IServiceCollection services)
{
...
// Maintain property names during serialization. See:
// https://github.com/aspnet/Announcements/issues/194
services
.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
// Add Kendo UI services to the services container
services.AddKendo();
}
Locate the Configure method and add a call to app.UseKendo at the end.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Configure Kendo UI
app.UseKendo(env);
}
更新
为了验证上述配置是否已应用,请检查服务器响应并查看其数据和总计是否具有以下结构和字母大小写:
{
"Data": [
{ "Id": "1", "Name": "Name 1" },
{ "Id": "2", "Name": "Name 2" }
],
"Total": "2"
}
关于postgresql - kendo ui grid 无法与 .net core 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39618669/