ASP.NET Core MVCプロジェクトでjquery-datatablesを学習しています。問題は、データ型がサーバー側のアクションメソッドを呼び出さないことです。Jqueryのデータ型はサーバー側でコントローラ内でアクションを呼び出さない
CrudController
[HttpPost]
public object GetStudents()
{
var test = student.GetAll(10, 0); //List<Student> the count result is 2
return new {
draw = 2,
recordsTotal = 2,
recordsFiltered = 2,
data = test
};
}
public IActionResult Index()
{
return View();
}
HTML for IActionResult Index
<table id="example">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Birthdate</th>
<th>Active</th>
</tr>
</thead>
</table>
Script for datatables inside $(document).ready
$("#example").DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url: "@Url.Action("GetStudents", "Crud")",
type: "POST"
},
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "birthdate" },
{ "data": "active" }
]
});
上記のコードはエラーを発生しません。しかし、GetStudents()
メソッドは呼び出されません(ブレークポイントを設定します)。何が問題なの?
url: "/ Crud/GetStudents" –
@AsifRazaの提案に基づいてURLを変更しましたか?コントローラとメソッドだけでなく、完全なURLを指定する必要があるかもしれません。 – markpsmith