Kendo UI网格未显示数据

时间:2015-03-19 00:13:33

标签: c# asp.net-mvc kendo-grid

我正在使用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;
        }

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

1 个答案:

答案 0 :(得分:0)

您需要更改您的&#34; GetGuarantorSummary&#34;行动结果如下:

 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 Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;

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

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

希望这能为您提供一种让您了解任何让我知道的问题的方法。