MVC 中的 Telerik.UI.for.AspNet.Core 无法从数据库上下文在网格中显示数据

发布于 2025-01-15 05:11:52 字数 2202 浏览 4 评论 0原文

我想我已经很接近了。它没有抛出任何错误,但也没有显示任何数据...我只是想让它显示我的 TblCompanyInfo 表中的公司名称和公司 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 行数据。但是,网格中没有显示任何内容。

输入图片此处描述

I think I'm close. Its not throwing any errors but its also not displaying any data... Im just trying to get it to display a list of Company Names and Company IDs from my TblCompanyInfo table.

This is my controller:

 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);
    }

and my view...

    @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()
        
    )

Also I know that the Products_Read function is being called by the view and I also know that the "result" contains 32 rows of data. However, nothing is displayed in the grid.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-01-22 05:11:52

想通了!事实证明,json 对返回字符串进行了驼峰命名,因此模型属性与 json 返回的内容不匹配。解决方案是将这一行添加到 Startup.cs 文件中。

services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);

Figured it out! Turns out that json camelcases the return string so the model properties did not match what was returned by json. The solution was to add this line to the Startup.cs file.

services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy = null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文