【问题标题】:Kendo Grid sorting with complex objects使用复杂对象进行 Kendo Grid 排序
【发布时间】:2015-04-26 12:48:01
【问题描述】:

我在读取剑道网格的数据时遇到问题。 控制器抛出异常“找不到要排序的原始类型的公共属性。”

视图模型

public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
{
    public int? Id { get; set; }
    public PersonalDataModel PersonalDataModel { get; set; }
}

控制器

public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
    var source = repository.All<PatientModel>();
    var patients = repository.All<PatientModel>().ToDataSourceResult(request);
    return Json(patients,JsonRequestBehavior.AllowGet);
 }

查看

@(Html.Kendo().Grid<DentalSoft.Data.Contracts.Patientes.PatientModel>()
.Name("PatientsGrid")    
.Columns(columns =>
{
    columns.Bound(p => p.PersonalDataModel.FirstName).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false)));
    columns.Bound(p => p.PersonalDataModel.SecondName).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false)));
    columns.Bound(p => p.PersonalDataModel.LastName).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false)));
    columns.Bound(p => p.PersonalDataModel.TelephoneNumber).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false)));
    columns.Bound(p => p.PersonalDataModel.HealthStatus).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(false))).Width(200);
    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
})
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("PatientModel"))
.ToolBar(toolbar => toolbar.Create())
.Scrollable()
.Sortable()
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
.HtmlAttributes(new { @class = "patients-grid" })
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Model(model => model.Id(p => p.Id))
    .Create(create => create.Action("EditingPopup_Create", "Grid"))
    .Read(read => read.Action("EditingPopup_Read", "Grid"))
    .Update(update => update.Action("EditingPopup_Update", "Grid"))
        .Destroy(destroy => destroy.Action("EditingPopup_Destroy", "Grid"))
)
.Selectable()
)

ViewModel 将有更多的 3,4 个复杂对象。

【问题讨论】:

    标签: kendo-ui kendo-grid kendo-asp.net-mvc


    【解决方案1】:

    搜索这个问题我找到了following article

    创建一个新类,它只包含所需的属性 数据绑定网格(属性映射到网格列)。 如果 绑定到 EF 确保您添加了密钥属性(在此 case) 到 ViewModel,即使您没有在网格中显示它。 否则你最终会得到 NotSupportedException 说“不能 查找要排序的原始类型或属性”。

    看起来这个问题是因为 repository.All&lt;PatientModel&gt;() 返回的任何东西都缺少 Entity Framework 所需的某些属性(您使用的是 Entity Framework 吗?)。

    您还可以尝试以下方法:

    public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
    {
        var patients = repository.All<PatientModel>().ToList();
        return Json(patients.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }
    

    【讨论】:

      【解决方案2】:

      将您的视图模型更改为

      public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
      {
          public int Id { get; set; }
          public PersonalDataModel PersonalDataModel { get; set; }
      }
      

      public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
      {
          public string Id { get; set; }
          public PersonalDataModel PersonalDataModel { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 2015-10-07
        • 2016-05-08
        • 1970-01-01
        • 2022-11-15
        相关资源
        最近更新 更多