I'm loading List of objects from database into datatable using ajax. When debugging, the my MVC action result seem to query the data alright but the datatable column displays null
I've tried to serialize the list before returning it in the MVC action but it didn't solve the problem
// Code from View
<table class="table table-striped" id="codetable">
<thead>
<tr>
<td>Student Number</td>
<td>Student</td>
<td>Faculty</td>
<td>Department</td>
<td>Program</td>
<td>Code</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
$(document).ready(function () {
$("#codetable").DataTable({
processing: true,
serverSide: true,
info: true,
ajax: {
url: '@Url.Action("GetVoters", "Index")',
dataSrc: ""
},
Columns: [
{ "data": "StudentNumber" },
{ "data": "Name" },
{ "data": "Faculty" },
{ "data": "Department" },
{ "data": "Program" },
{ "data": "Code" }
]
});
});
</script>
//Code from Controller
public JsonResult GetVoters()
{
List<vt> stud = (from student in _context.Voters
select new vt
{
StudentNumber = student.StudentNumber,
Name = student.Name,
Faculty = student.Faculty,
Department = student.Department,
Program = student.Program,
Code = student.Code
}).Take(100).ToList();
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = js.Serialize(stud);
return Json(result, JsonRequestBehavior.AllowGet);
}
public class vt
{
public string StudentNumber { get; set; }
public string Name { get; set; }
public string Faculty { get; set; }
public string Department { get; set; }
public string Program { get; set; }
public string Code { get; set; }
}
I expect the table to display various columns in the list but is throws this error "DataTables warning: table id=codetable - Requested Unknown parameter '1' for row 0, column 1..." and displays results only in the first column(thus a character per row). The rest of the columns show null