Telerik.UI.for.AspNet.Core 在 MVC 中无法显示来自数据库上下文的网格中的数据
Telerik.UI.for.AspNet.Core in MVC Cannot display data in Grid from a DB context
我想我很接近。它没有抛出任何错误,但也没有显示任何数据...我只是想让它显示来自我的 TblCompanyInfo table.
的公司名称和公司 ID 列表
这是我的控制器:
public async Task<IActionResult> Index()
{
var apptReminderContext = _context.TblCompanyInfos.Include(t => t.AcctType).Include(t => t.CompanyStatus).Include(t => t.OnHoldReason);
return View(await apptReminderContext.ToListAsync());
//return View();
}
public JsonResult Products_Read([DataSourceRequest] DataSourceRequest request)
{
DataSourceResult result = _context.TblCompanyInfos.ToDataSourceResult(request,
model => new TblCompanyInfo
{
CompanyId = model.CompanyId,
CompanyName = model.CompanyName
});
return Json(result);
}
以及我的观点...
@model IEnumerable<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
@using AppointmentRemindersNetCoreMVC.Data
@using Kendo.Mvc.UI
@addTagHelper *, Kendo.Mvc
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
@(Html.Kendo().Grid<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>()
.Name("grid")
.DataSource(dataSource => dataSource.Ajax()
.Read(read => read.Action("Products_Read", "Company"))
.PageSize(20)
//.ServerOperation(false)
//.Model(model => model.Id(c => c.CompanyId))
//.Read("Products_Read", "Company")
//.Read(read => read.Action("Products_Read", "Company"))
.Update("UpdateCustomer", "Home")
.Create("InsertCustomer", "Home")
.Destroy("DeleteCustomer", "Home"))
.Columns(columns =>
{
columns.Bound(product => product.CompanyName);
columns.Bound(product => product.CompanyId);
})
.Pageable()
.Sortable()
)
我还知道视图正在调用 Products_Read 函数,我也知道“结果”包含 32 行数据。但是,网格中没有显示任何内容。
想通了!结果是 json 驼峰式 return 字符串,因此模型属性与 json 编辑的 return 不匹配。解决方案是将此行添加到 Startup.cs 文件。
services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);
我想我很接近。它没有抛出任何错误,但也没有显示任何数据...我只是想让它显示来自我的 TblCompanyInfo table.
的公司名称和公司 ID 列表这是我的控制器:
public async Task<IActionResult> Index()
{
var apptReminderContext = _context.TblCompanyInfos.Include(t => t.AcctType).Include(t => t.CompanyStatus).Include(t => t.OnHoldReason);
return View(await apptReminderContext.ToListAsync());
//return View();
}
public JsonResult Products_Read([DataSourceRequest] DataSourceRequest request)
{
DataSourceResult result = _context.TblCompanyInfos.ToDataSourceResult(request,
model => new TblCompanyInfo
{
CompanyId = model.CompanyId,
CompanyName = model.CompanyName
});
return Json(result);
}
以及我的观点...
@model IEnumerable<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
@using AppointmentRemindersNetCoreMVC.Data
@using Kendo.Mvc.UI
@addTagHelper *, Kendo.Mvc
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
@(Html.Kendo().Grid<AppointmentRemindersNetCoreMVC.Models.TblCompanyInfo>()
.Name("grid")
.DataSource(dataSource => dataSource.Ajax()
.Read(read => read.Action("Products_Read", "Company"))
.PageSize(20)
//.ServerOperation(false)
//.Model(model => model.Id(c => c.CompanyId))
//.Read("Products_Read", "Company")
//.Read(read => read.Action("Products_Read", "Company"))
.Update("UpdateCustomer", "Home")
.Create("InsertCustomer", "Home")
.Destroy("DeleteCustomer", "Home"))
.Columns(columns =>
{
columns.Bound(product => product.CompanyName);
columns.Bound(product => product.CompanyId);
})
.Pageable()
.Sortable()
)
我还知道视图正在调用 Products_Read 函数,我也知道“结果”包含 32 行数据。但是,网格中没有显示任何内容。
想通了!结果是 json 驼峰式 return 字符串,因此模型属性与 json 编辑的 return 不匹配。解决方案是将此行添加到 Startup.cs 文件。
services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);