Kendo UI 网格未显示数据

Kendo UI grid is not showing data

我正在使用 MVC Kendo 网格并且无法填充网格。这是cshtml代码

@(Html.Kendo().Grid<EntryPointCRR.Models.GuarantorInfo>()
        .Name("kgGuarantor")
        .Columns(c =>
        {
            //  c.Bound(p => p.Id);
            c.Bound(p => p.Rank);
            c.Bound(p => p.GuarantorNameModel.Name);
            c.Bound(p => p.GuarantorNameModel.CisNumber);
            c.Bound(p => p.Type);
            c.Bound(p => p.GuaranteeShare);
            c.Template(@<text></text>).ClientTemplate(@"<a class=""k-button k-button-icontext k-grid-edit"" href=""\#""><span class=""fa fa-pencil""></span></a><a class=""k-button-icontext k-grid-delete"" href=""\#""><span class=""fa fa-trash-o""></span></a>");
        })
             .ToolBar(toolBar =>
             {
                 toolBar.Create().Text("Add Guarantor");
             })
            .Editable(editable => editable.Mode(GridEditMode.PopUp))
            .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
            .DataSource(dataSource => dataSource.Ajax()
                        .ServerOperation(false)
                        .Model(model => model.Id(p => p.Id))
                        .Create(create => create.Action("AddGuarantors", "Guarantor"))
                                            .Read(read => read.Action("GetGuarantorSummary", "Guarantor", new { packageId = 1, packageProductId = 1 }))

            )
        )

这是我的控制器代码

public ActionResult GetGuarantorSummary(string packageId, string packageProductId)
        {
            GuarantorModel objGuarantorModel = new GuarantorModel();
            try
            {
                //Guarantors objGuarantors = new Guarantors(packageId, packageProductId);
               // objGuarantorModel = objGuarantors.GetGuarantorsSummary();
                objGuarantorModel = GetGuarantorsStubbedData();
            }
            catch (Exception ex)
            {
                Logger.Write(ex, "General", 1, 1, System.Diagnostics.TraceEventType.Error);
            }
            return View("Guarantor", objGuarantorModel);
        }

  private GuarantorModel GetGuarantorsStubbedData()
        {
            GuarantorModel objGuarantorModel = new GuarantorModel();
            objGuarantorModel.GuarantorDetails.Add(new GuarantorInfo
            {
                Id = 1,
                Rank = "1",
                Type = "Full",
                GuaranteeShare = "30%",
                GuarantorNameModel = new GuarantorNameModel
                {
                    Name = "Acme",
                    CisNumber = "12345"
                }
            });
            objGuarantorModel.GuarantorDetails.Add(new GuarantorInfo
            {
                Id = 2,
                Rank = "3",
                Type = "Full",
                GuaranteeShare = "40%",
                GuarantorNameModel = new GuarantorNameModel
                {
                    Name = "Acme Company",
                    CisNumber = "123456"
                }
            });

            return objGuarantorModel;
        }

我只能看到网格 header 而不是数据。 你能帮我解决这个问题吗?

您需要根据以下内容更改您的 "GetGuarantorSummary" 操作结果:

 public JsonResult GetGuarantorSummary([DataSourceRequest] DataSourceRequest request, string packageId, string packageProductId)
        {
           // GuarantorModel objGuarantorModel = new GuarantorModel();
            try
            {
                //Guarantors objGuarantors = new Guarantors(packageId, packageProductId);
                // objGuarantorModel = objGuarantors.GetGuarantorsSummary();
                //objGuarantorModel = GetGuarantorsStubbedData();
                List<EntryPointCRR.Models.GuarantorInfo> model = new List<EntryPointCRR.Models.GuarantorInfo>();
                //get your collection here:
                //TODO: My collection 
            }
            catch (Exception ex)
            {
                Logger.Write(ex, "General", 1, 1, System.Diagnostics.TraceEventType.Error);
            }
            return Json(model.ToDataSourceResult(request, ModelState), JsonRequestBehavior.DenyGet);
        }

要进行此更改,请确保您具有以下 using 语句

using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;

没有这些,DataSourceRequest 模型和扩展方法将无法工作。

将您的对象返回到网格时,请确保您发送回一个集合,因为这是网格所期望的,并且正如您所指出的那样,网格期望模型类型 EntryPointCRR.Models.GuarantorInfo 确保这是被发送回网格的模型类型,否则您将得到类型不匹配。

希望这为您提供了一种入门方式,如有任何问题请告诉我。