ekondur / DatatableJS

Jquery datatable with entity framework using MVC html helper
MIT License
36 stars 15 forks source link

Data Not Showing up when use include clause in query #141

Closed nazerulgit closed 3 months ago

nazerulgit commented 3 months ago

Hi @ekondur i have this query in my repository

public IQueryable GetDataResult() { return _repository.GetDataResult().Include(p => p.Person); }

And i use it in my controller

[HttpPost] public JsonResult GetDataResult(DataRequest request) {
DataResult result = _repo.GetDataResult().ToDataResult(request); return Json(result); }

to showing up this data in my views @(Html.JS().Datatable() .Class("table table-hovered table-striped dataTable") .Name("datatable") .LengthMenu(new int[] { 5, 10, 25, 50, 100 }, true, "All Pages") .FixedColumns(leftColumns: 0, rightColumns: 1) .ColumnSearching(true, "form-control", SearchPosition.Header) .Columns(cols => { cols.Field(a => a.tch_Id).Class("w-175px").Orderable(false).Searchable(false).Title("Actions").Template("'<div class=\"btn-group\"> <a class=\"btn btn-success px-4 mx-1\" onclick=\"goEdit('+row.tch_Id+')\" href=javascript:void(0)><i class=\"las la-pen-alt fs-5\"> Edit <a class=\"btn btn-danger px-4\" onclick=showingPrompt(\"./teacher/Delete/'+row.tch_Id+'\") href=javascript:void(0)><i class=\"las la-trash\"> Delete

'"); cols.Field(a => a.tch_Id).Title("Id"); cols.Field(a => a.Person.PerNric).Title("Nric"); cols.Field(a => a.Person.PerName).Title("Name"); cols.Field(a => a.Person.PerSex).Title("Sex"); cols.Field(a => a.Person.PerTelMobile).Title("Mobile"); cols.Field(a => a.tch_DateJoin).Title("Date Join"); cols.Field(a => a.tch_FullTime).Title("Full Time"); cols.Field(a => a.tch_Notes).Title("Notes"); }) .ColReorder(true) .URL(Url.Action("GetDataResult"), "POST") .ServerSide(true) .StateSave(true) .Orders(order => { order.Add(p => p.tch_Id, OrderBy.Descending); }) .Render() )

all data in my Teacher Model is showing up but all data in my include model is not

when i check in my html the datatables script is like this

columns: [{ --   | data: 'tch_Id',   | name: 'tch_Id',   | defaultContent: '',   | orderable: false,   | searchable: false,   | className: 'w-175px',   | visible: true,   | width: '',   | 'render': function(data, type, row, meta) { return '
Edit Delete
'; }   | }, {   | data: 'tch_Id',   | name: 'tch_Id',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'PerNric',   | name: 'PerNric',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'PerName',   | name: 'PerName',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'PerSex',   | name: 'PerSex',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'PerTelMobile',   | name: 'PerTelMobile',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'tch_DateJoin',   | name: 'tch_DateJoin',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'tch_FullTime',   | name: 'tch_FullTime',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }, {   | data: 'tch_Notes',   | name: 'tch_Notes',   | defaultContent: '',   | orderable: true,   | searchable: true,   | className: '',   | visible: true,   | width: '',   |     | }]   | })

Column PerName, PerNric is from Person Model so the scripts should Person.PerName, Person.PerNric etc

when i change my html using raw html from Generated DatatableJS and change the column with the right column then the data is showing, but when i try to search using include column then the datatable will showing up with the error Instance property 'Person.PerName' is not defined for type 'mrs.Models.Teacher'

Regards Nasrul

ekondur commented 3 months ago

Hi @nazerulgit It doesn't support object types for performance reasons. Please use a model that includes only primitive types and strings.

nazerulgit commented 3 months ago

Okay, thank you.

Regards Nasrul