2015-04-26 54 views
0

我在讀取劍道網格數據時遇到了問題。 控制器拋出異常「找不到原始類型的公共屬性進行排序。」Kendo使用複雜對象進行網格排序

視圖模型

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

視圖模型將有更多3,4個複雜的對象。

回答

0

搜索這個問題,我發現following article

創建一個新的類,將只包含 所需數據綁定的網格(映射到網格列是屬性)的屬性。 如果 綁定到EF,請確保將關鍵屬性(此 大小寫中的OrderID)添加到ViewModel,即使您沒有在網格中顯示它。 否則,您將以NotSupportedException爲結尾說「不能 找到要排序的原始類型或屬性」。

它看起來像(您正在使用實體框架?)會出現此問題,因爲無論是repository.All<PatientModel>()返回缺少這是由實體框架所需的某些屬性。

您也可以嘗試以下方法:

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

您的視圖模型更改爲

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; } 
} 
相關問題