Здравствуйте! Использую
DataTables плагин и с помощью ajax хочу отобразить таблицу, передав данные в json.
Метод контроллера, который возвращает json:
public string GroupReportPartial()
{
using (AcademicProgressDb db = new AcademicProgressDb())
{
var query = (from Students in db.Students
from StudentsSubjects in db.StudentsSubjects
from Subjects in db.Subjects
from ControlPeriods in db.ControlPeriods
from ControlTypes in db.ControlTypes
where
Students.RecordBookNumber == StudentsSubjects.StId.ToString()
where
Subjects.SubjectId == StudentsSubjects.SubjId
where
ControlPeriods.ControlPeriodId == StudentsSubjects.CtrlPeriodId
where
ControlTypes.ControlTypeId == StudentsSubjects.CtrlTypeId
select new GroupsReportModel
{
StudentGroupNumber = Students.GroupNumber,
StudentLastName = Students.LastName,
StudentFirstName = Students.FirstName,
StudentMiddleName = Students.MiddleName,
SubjectName = Subjects.Name,
ControlPeriodName = ControlPeriods.Name,
ControlTypeName = ControlTypes.Name
}).ToList();
return JsonConvert.SerializeObject(query);
}
}
Работает успешно. В результате я получаю:
[{
"StudentGroupNumber": 2012,
"StudentLastName": "Глобчук",
"StudentFirstName": "Даниил",
"StudentMiddleName": "Владимирович",
"SubjectName": "Иностранный язык",
"ControlPeriodName": "Второй семестр",
"ControlTypeName": "Зачет"
}]
Код в представлении:
<script type="text/javascript">
$(document).ready(function () {
$('#example').DataTable({
ajax: {
url: '@Url.Action("GroupReportPartial")',
type: 'POST',
dataType: 'json',
dataSrc: "",
columns: [
{ data: "StudentGroupNumber", name:"StudentGroupNumber" },
{ data: "StudentLastName", name: "StudentLastName" },
{ data: "StudentFirstName", name: "StudentFirstName" },
{ data: "StudentMiddleName", name: "StudentMiddleName" },
{ data: "SubjectName", name: "SubjectName" },
{ data: "ControlPeriodName", name: "ControlPeriodName" },
{ data: "ControlTypeName", name: "ControlTypeName" }
]
}
});
});
</script>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>StudentGroupNumber</th>
<th>StudentLastName</th>
<th>StudentFirstName</th>
<th>StudentMiddleName.</th>
<th>SubjectName</th>
<th>ControlPeriodName</th>
<th>ControlTypeName</th>
</tr>
</thead>
</table>
Казалось бы всё хорошо, но получаю ошибку
DataTables warning: table id=example - Requested unknown parameter '0' for row 0, column 0
Количество столбцов в json и во view одинаковое. В чем может быть причина?