データテーブルを使用して詳細テーブルを表示しようとしましたが、次のエラーが発生しました。私は、次の表を使用しています。このコントローラのビューでC#mvcのデータ型は、Item内の各varに期待していません。
public ActionResult JsonDetails(int Id)
{
Equipment e = _repo.GetSingle(Id);
var model = AutoMapper.Mapper.Map<Equipment, EquipmentDetailsViewModel>(e);
JsonResult jsonModel = new JsonResult()
{
Data = model,
ContentEncoding = System.Text.Encoding.UTF8,
ContentType = "application/json; charset=utf-8",
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
MaxJsonLength = Int32.MaxValue
};
return jsonModel;
}
public ActionResult Details(int Id)
{
return View();
}
:
@using Project.ViewModels;
@model EquipmentDetailsViewModel
<table id="dataTable" class="datatables table table-striped table-hover ">
<thead>
<tr>
<th>
Service No
</th>
<th>
Service Due Date
</th>
<th>
Service Completed Date
</th>
</tr>
</thead>
<tbody>
私のViewModelはこのようなこの
public class EquipmentDetailsViewModel
{
public int Equipment_Id { get; set; }
public IEnumerable<EquipmentServiceHistoryListViewModel> Service_History { get; set; }
}
私のコントローラの外観のように見える。このため enter image description here
および
@foreach (var item in Model.Service_History)
{
<tr>
<td>
@Html.DisplayFor(model => item.Service_No)
</td>
<td>
@Html.DisplayFor(model => item.Sevice_Due_Date)
</td>
<td>
@Html.DisplayFor(model=> item.Service_Completed_Date)
</td>
</tr>
}
</tbody>
</table>
私のJSは、誰もが、私はこのページを動作させるために使用できる項目 の各VARのためにどのような選択肢を私にお勧めできます
@section scripts{
<script>
$(document).ready(function() {
$('.datatables').DataTable({
"lengthMenu": [[25, 50, -1], [25, 50, "All"]],
"ajax": {
"url": '/Service_History/JsonDetails/',
"dataSrc": ""
},
"deferRender": true,
"columns": [
{
data: 'Service_No',
visible: false
},
{ data: 'Service_Due_Date',
render: function (data, type, row) {
if (type === 'display' || type === 'filter') {
return (moment(data).format("DD/MM/YYYY"));
}
return data;
}
},
{
data: 'Service_Completed_Date',
render: function (data, type, row) {
if (type === 'display' || type === 'filter') {
return (moment(data).format("DD/MM/YYYY"));
}
return data;
}
}
],
"order": [] //prevent auto sorting on first column
});
});
</script>
}
以下のような設定のものです。 ありがとう
これはエラーメッセージではありません。これは単なるコード行への参照です。どのような状況でそれを見ていますか?実際の問題は何ですか? – David
あなたはjsonの結果を返しています。ビューは私が見ることのできるモデルから提供されていないので、Service_Historyはインスタンス化されません。私は全体の画像を持っているとは思わないので、これをコメントとして残しておきます。あなたは、ビューモデルを設定している場所を詳しく説明することができますか? – SCFi
'public ActionResult Details(int Id) { 戻り値View(); } 'はモデルデータを返しません。これは、ページの読み込み時に使用されるモデルを宣言する場所です。 foreach Razor構文は、このメソッドで(必要な)モデルデータに基づいてサーバーによって解釈されます。私は、JsonDetailsメソッドがいくつかのajax呼び出しでのみ使用されると仮定しているので、別の時間に呼び出されます。いくつかのブレークポイントを設定し、コード内で実際に何が実行されているかを確認します。 – ADyson