Answer the question
In order to leave comments, you need to log in
How to populate a table with ajax?
Hello! I use the DataTables plugin and using ajax I want to display the table by passing the data in json.
Controller method that returns 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
Answer the question
In order to leave comments, you need to log in
so stop, shouldn't the columns field be outside the ajax field. something like this:
ajax:
{
// параметры запроса
},
columns:
[ {
// параметры столбца.
}, { } ... ]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question