Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
public JsonResult GetData()
       {
           Employee emp = new Employee();
           List<Employee> emplist = new List<Employee>();
           emp.name = "Durgesh Mapuskar";
           emp.salary = "500000";
           emp.position = "Team Lead";
           emp.start_date = "01/01/2016";
           emp.office = "BUPA";
           emp.extn = 4156;
           emplist.Add(emp);
      var json = JsonConvert.SerializeObject(emplist);
       return Json(json.ToArray(), JsonRequestBehavior.AllowGet);
     }


$(document).ready(function () {
        debugger;
        var urllink = "/Home/GetData";
        var table = $('#example').DataTable({            
            ajax: {
                url: urllink,
                
            },
            "columns": [
                {
                    "className": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": ''
                },
                { "data": "name" },
                { "data": "position" },
                { "data": "office" },
                { "data": "salary" }
            ],
            "order": [[1, 'asc']]
        });


What I have tried:

it gives me length property not defined
Posted
Updated 19-Jun-18 1:45am

1 solution

The Json helper method[^] expects the object you want to return, not an already-serialized JSON string.

Remove the call to JsonConvert.SerializeObject, and just pass in the list:
C#
public JsonResult GetData()
{
    List<Employee> emplist = new List<Employee>();
    
    ...
    
    return Json(emplist, JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
Comments
Abrar Kazi 15-Jun-17 1:07am    
Thanks.

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



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