Reputation: 35
I have a problem with read of data for kendo grid. The controller throw a exception "Cannot find a public property of primitive type to sort by."
ViewModel
public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
{
public int? Id { get; set; }
public PersonalDataModel PersonalDataModel { get; set; }
}
Controller
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
var source = repository.All<PatientModel>();
var patients = repository.All<PatientModel>().ToDataSourceResult(request);
return Json(patients,JsonRequestBehavior.AllowGet);
}
View
@(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()
)
The ViewModel will has more 3,4 complex objects.
Upvotes: 1
Views: 1638
Reputation: 1499
Change your viewmodel to
public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
{
public int Id { get; set; }
public PersonalDataModel PersonalDataModel { get; set; }
}
or
public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
{
public string Id { get; set; }
public PersonalDataModel PersonalDataModel { get; set; }
}
Upvotes: 0
Reputation: 12846
Searching for this issue I found the following article:
Create a new class which will contain only the properties required for databinding the grid (properties mapped to grid columns that is). If binding to EF make sure you add the key property (OrderID in this case) to the ViewModel even if you are not displaying it in the grid. Otherwise you will end up with NotSupportedException saying “Cannot find primitive type or property to sort by”.
It looks like this issue occurs because whatever repository.All<PatientModel>()
is returning is missing some property which is required by Entity Framework (are you using Entity Framework?).
You can also try the following:
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
var patients = repository.All<PatientModel>().ToList();
return Json(patients.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
Upvotes: 1