Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have a jQuery DataTable with server-side processing which is giving me circular dependency issue.

I have to list a field `Administrator` in my DataTable that belongs to another class. If I include it in the code, I get dependency errors with Ajax like

> DataTables warning: table id=datatableServer-Ajax error.

Is there a way to fix this issue?

What I have tried:

**Department class**

public class Department
   {
       public int DepartmentID { get; set; }

       [StringLength(50, MinimumLength = 3)]
       public string Name { get; set; }

       [DataType(DataType.Currency)]
       [Column(TypeName = "money")]
       public decimal Budget { get; set; }

       [DataType(DataType.Date)]
       [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
       [Display(Name = "Start Date")]
       public DateTime StartDate { get; set; }

       [Display(Name = "Administrator")]
       public int? InstructorID { get; set; }

       [Timestamp]
       public byte[] RowVersion { get; set; }

       public virtual Instructor Administrator { get; set; }
       public virtual ICollection<Course> Courses { get; set; }
   }


**Instructor**
public class Instructor : Person
    {
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Hire Date")]
        public DateTime HireDate { get; set; }

        public virtual ICollection<Course> Courses { get; set; }
        public virtual OfficeAssignment OfficeAssignment { get; set; }
    }


**Controller**

public ActionResult DataHandler([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
    {
        //IQueryable<Department> query = departmentService.GetDepartments();

        IQueryable<Department> query;

        using (SchoolContext db = new SchoolContext())
        {
            query = db.Departments;

            var totalCount = query.Count();

            // Apply filters for searching
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.Name.Contains(value) 
                //|| p.Administrator.FullName.Contains(value));                                                                                                  
            }

            var filteredCount = query.Count();

            // Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = String.Empty;

            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != String.Empty ? "," : "";
                orderByString += (column.Data) +
                  (column.SortDirection ==
                  Column.OrderDirection.Ascendant ? " asc" : " desc");
            }

            query = query.OrderBy(orderByString ==
            string.Empty ? "BarCode asc" : orderByString);

            // Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length);

            var data = query.Select(a => new
            {
                Name = a.Name,
                Budget = a.Budget,
                StartDate = a.StartDate,
                //Administrator = a.Administrator.FullName
            }).ToList();

            return Json(new DataTablesResponse
            (requestModel.Draw, data, filteredCount, totalCount),
                        JsonRequestBehavior.AllowGet);
        }
    }


**Script**

$(document).ready(function () {

        $('#datatable').dataTable();  //client side datatable

        var departmentsList;
        $(function () {
            departmentsList = {
                dt: null,

                init: function () {
                    //Server-side dataTable
                    dt = $('#datatableServer').DataTable({
                        "serverSide": true,
                        "processing": true,
                        "ajax": {
                            "url":
                            "@Url.Action("DataHandler","Department")"
                        },
                        "columns": [
                            { "data": "Name",
                            "searchable": true },
                            { "data": "Budget",
                            "searchable": true },
                            { "data": "StartDate",
                            "searchable": true }
                            //{ "data": "Administrator",
                            //"searchable": true }
                        ],
                        "lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
                    });
                }
            }
            // initialize the datatables
            departmentsList.init();
        });
    });
Posted
Updated 21-Jun-22 9:31am
v3
Comments
Graeme_Grant 14-Mar-17 21:12pm    
Have you tried using a debugger and stepping through your code to see why?
Member 13059365 14-Mar-17 21:39pm    
Yes, I have. I spent 2 days with this error message. And have come to conclusion that It is because my Department class has a Navigation Property for the Administrator(Instructor) class. I have to get the Administrator's full name in my DataTable. When I include that field, I get a "Datatable warning: -Ajax error" when I do search or sort something on the dataTable.
But when I don't include the Administrator's name field, it works fine.
Graeme_Grant 14-Mar-17 22:49pm    
I'm not familiar with the jQuery DataTable however, after googling around, I get the feeling that the response coming back from the server is not what the ajax call is expecting - no data, too much data, wrong format, etc... If you use Chrome dev tools and inspect the conversation with the server for a good call versus the bad call that you are experiencing, you should be able to identify the root cause of your problem.
Ehsan Sajjad 15-Mar-17 5:19am    
you don't need to include it directly in select, instead project it's specific properties as you did but i can see it is commented out which is :
Administrator = a.Administrator.FullName
[no name] 15-Mar-17 8:53am    
I included
Administrator = a.Administrator.FullName
for my
var data
, but I am getting this error message,

The specified type member 'FullName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

1 solution

FullName is not an entity member(i.e. column name for the Person table), for this reason the query is not able to run. Can you try with LastName only. If that works then you can use <LastName + ", " + FirstMidName>.
For sorting we have multiple ways of blocking it. You can use
{ "data": "Administrator", "searchable": true , "orderable": false}
Please refer columns.orderable[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900