ASP.NET COREMVCでのjQueryDataTablesグリッドの使用には、CoreMVCでJQueryDatatablesを使用する方法に関する良い例があります。
サンプルプロジェクトをダウンロードし、いくつかの変更を加えましたが、完全に機能します。
ただし、これをプロジェクトに統合しようとするとエラーが発生します。
DataTablesの警告:table id = example-行0、列0に不明なパラメーター 'IdStudent'を要求しました。このエラーの詳細については、http: //datatables.net/tn/4を参照してください。
エラーで[OK]をクリックすると、テーブルは正しい行数で生成されますが、データはありません。
これは私のクラスです:
public class GridStudent
{
[Key]
public int IdStudent { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
HTMLとJavaScript:
<div class="container">
<br />
<div style="width:90%; margin:0 auto;">
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
<thead>
<tr>
<th>IdStudent</th>
<th>Name</th>
<th>LastName</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
</table>
</div>
</div>
<script>
$(document).ready(function ()
{
$("#example").DataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "/StudentGrid/LoadData",
"type": "POST",
"datatype": "json"
},
"columnDefs":
[
{
"targets": [0],
"visible": false,
"searchable": false,
}
],
"columns": [
{ "data": "IdStudent", "name": "IdStudent", "autoWidth": true },
{ "data": "Name", "name": "Name", "autoWidth": true },
{ "data": "LastName", "name": "LastName", "autoWidth": true },
{
"render": function (data, type, full, meta)
{ return '<a class="btn btn-info" href="/StudentGrid/Edit/' + full.IdStudent + '">Edit</a>'; }
}
,
{
data: null, render: function (data, type, row)
{
return "<a href='#' class='btn btn-danger' onclick=DeleteData('" + row.IdStudent + "'); >Delete</a>";
}
},
]
});
});
function DeleteData(CustomerID)
{
if (confirm("Are you sure you want to delete ...?"))
{
Delete(CustomerID);
}
else
{
return false;
}
}
function Delete(CustomerID)
{
var url = '@Url.Content("~/")' + "StudentGrid/Delete";
$.post(url, { ID: CustomerID }, function (data)
{
if (data)
{
oTable = $('#example').DataTable();
oTable.draw();
}
else
{
alert("Something Went Wrong!");
}
});
}
</script>
これは、データを返すコントローラーからのコード行です。
return await Task.Run(() => Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }));
画像からわかるように、コントローラーはデータを正しく返していますが、何らかの理由でDataTablesはデータを読み取ることができません。
サンプルと1000回クロスチェックしましたが、返されるデータの形式に違いが見られません。
助言がありますか?
これは、シリアル化されたJSONのケーシングが原因である可能性があります。あなたのdata
あなたのJavaScript内のカラム定義のプロパティは、パスカルケーシングを期待しています。現在、JSONをPascalケースではなく小文字のキャメルケースとしてシリアル化していると思います(たとえば、idStudent
ではなくIdStudent
)。
JSONをPascalケースとしてシリアル化ConfigureServices
するには、Startup
クラスのメソッドに次のものがあることを確認してください。
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加