Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working with the DataTable jquery plugin inside my asp.net mvc web application.

i have the following model class:-

public partial class Emp
    {
        public int EmpID { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public Nullable<int> DeptID { get; set; }

        public virtual Dept Dept { get; set; }
    }

the following script:-

 <script type="text/javascript">
        $(document).ready(function () {

            $('#myDataTable').dataTable({
                "bServerSide": true,
                "sAjaxSource": "Home/AjaxHandler",
                "bProcessing": true,
                "aoColumns": [
                                { "sName": "FName"
                                    }
                                ,
                                { "sName": "LName" },
                                { "sName": "DeptID" }
                ]
            });
        });
    </script>

the following controller:-

    public ActionResult AjaxHandler(jQueryDataTableParamModel param)
    {
        var allCompanies = t.Emps;

        var result = allCompanies.Select(c=> new {c.FName, c.LName, c.DeptID});
                   //  select new[] {  };

        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = allCompanies.Count(),
            iTotalDisplayRecords = allCompanies.Count(),
            aaData = result.ToList()
        },
                        JsonRequestBehavior.AllowGet);
    }

and here is the view:-

<table id="myDataTable" class="display">
    <thead>
        <tr>
            <th>FName</th>
            <th>LName</th>
            <th>DeptID</th>

        </tr>
    </thead>
    <tbody></tbody>
</table> 

but when i run the application i got the following error and all the data will be displayed as null:-

DataTables warning: table id=myDataTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

DataTables warning: table id=myDataTable - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4

enter image description here

share|improve this question
    
what does returned JSON look like for aData? Seems it doesn't match what you declared in column definitions –  charlietfl Jan 5 at 14:53
    
@charlietfl can you adivce more on this please ? now i am returning three columns from my action method, as defined inside the columns definition.. –  john G Jan 5 at 14:57
1  
I think problem might be using old API docs to set up column definitions. Example from one of my projects: columns:[ { data: "id" }.. –  charlietfl Jan 5 at 15:16
1  
using data instead of sName? –  charlietfl Jan 5 at 15:27
1  
lots of examples in the docs, as well as API is well documented –  charlietfl Jan 5 at 15:39

1 Answer 1

As stated you are using old initializers, try this instead

$('#myDataTable').dataTable({
    "serverSide": true,
    "ajax": "Home/AjaxHandler",
    "processing": true,
    "columns": [
                { "data": "FName"},
                { "data": "LName" },
                { "data": "DeptID" }
    ]
});
share|improve this answer
    
ok it worked , but i am facing a problem when displaying a Date field inside my datTable plugin. for example i have a dattime field with value equal to "01/01/1980 01:02:04" but it will be dislayed as "/Date(315536524000)/" inside the dataTable –  john G Jan 5 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.