MVC and Ajax datatable with JSON

Jun 29 2018 5:27 AM
Hi Sir,
 
I have a controller which sends json data to a view and the view supposed to grab that json data and display in the datatable Ajax. but instead view is displaying just RAW json data.
 
my Controller code is -
  1. public ActionResult Index2()  
  2. {  
  3.   
  4. using (Model_Test dbObj = new Model_Test())  
  5. {  
  6. List<Test_Table> testList = dbObj.Test_Table.ToList<Test_Table>();  
  7. var obj = new { data = testList };  
  8. return Json(obj, JsonRequestBehavior.AllowGet);  
  9. }  
  10. }  
my View code is -
  1. @{  
  2. ViewBag.Title = "Index";  
  3. Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5. <br />  
  6. <br />  
  7. <table id="abc" class="display">  
  8. <thead>  
  9. <tr>  
  10. <th>Id</th>  
  11. <th>Name</th>  
  12. <th>Address</th>  
  13. </tr>  
  14. </thead>  
  15. </table>  
  16. <br />  
  17. <br />  
  18. <link href="~/Content/jquery.dataTables.min.css" rel="stylesheet" />  
  19. @section Scripts{  
  20. <script src="~/Scripts/jquery.dataTables.min.js"></script>  
  21. <script>  
  22. $(document).ready(function () {  
  23. $('#abc').DataTable({  
  24. "ajax": {  
  25. "url""/Test_Table/Index2",  
  26. "type""GET",  
  27. "datatype""json"  
  28. },  
  29. "columns": [  
  30. "data""Id""autoWidth"true },  
  31. "data""Name""autoWidth"true },  
  32. "data""Address""autoWidth"true }  
  33. ]  
  34. });  
  35. });  
  36. </script>  
  37. }  
I tested the datatable with static data and it works fine. so it should be something to do with controller json output. Please help me I am so stuck.
 
Thanks

Answers (3)