データの表示に問題があります。ASP.NETコア - データの表示エラー
私が私のページで達成しようとしているのは、データを見るために1つのビューにすべてを置き、ブートストラップを使ってCRUD操作を行うことです。私はこの種のエラー受信
:また
を、それは私が新しいインスタンスを作成する必要があることを述べたので、私は)(新しいを使用する必要があることが示唆されたが、私はどこに知りませんそれを置く。私はpublic lstEmployee = new IEnumerable<Employee> { get; set; }
のような私のモデルに入れようとしましたが、まったく動作していないようです。
これは私のコードです:
モデル:
using PEMCOLoan.DAL.Entities.Models;
namespace prjPEMCOLoan.Models
{
public class ModelEmployee
{
public IEnumerable<Employee> lstEmployee { get; set; }
public Employee employees { get; set; }
}
}
ビュー:
@model prjPEMCOLoan.Models.ModelEmployee
@{
ViewBag.Title = "List of Employees";
}
<form class="navbar-form navbar-right" style="margin-bottom: 10px;">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<p>
<h3>List of Employees</h3>
<button type="button" class="btn btn-info btn-sm openAdd" data-toggle="modal" data-target="#myModal2"><span class="glyphicon glyphicon-floppy-save" style="vertical-align:middle;margin-top: -5px"></span> Add</button>
</p>
<table class="table table-hover table-striped">
<tr>
<th>ID Employee</th>
<th>Full Name</th>
<th>Contact Number</th>
<th>Position</th>
<th>Action</th>
</tr>
@foreach (var item in Model.lstEmployee)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.EmployeeId)</td>
<td>@Html.DisplayFor(modelItem => item.Fname) @Html.DisplayFor(modelItem => item.Lname)</td>
<td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => item.Position)</td>
<td>
<a class="btn btn-primary btn-sm" asp-controller="Employee" asp-action="Details" asp-route-id="@item.EmployeeId"><span class="glyphicon glyphicon-dashboard" style="vertical-align:middle;margin-top: -5px"></span> Details</a>
<button type="button" class="btn btn-warning btn-sm openEdit" data-toggle="modal" data-target="#myModal" data-emp-id="@item.EmployeeId"><span class="glyphicon glyphicon-edit" style="vertical-align:middle;margin-top: -5px"></span> Edit</button>
<button type="button" class="btn btn-danger btn-sm openDiag" data-toggle="modal" data-target="#myModal" data-emp-id="@item.EmployeeId"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
</td>
</tr>
}
</table>
コントローラー:
[HttpGet]
public async Task<IActionResult> Index()
{
var getAllEmployees = await _Context.Employee.ToListAsync();
return View(getAllEmployees);
}
エラーは非常に明確です:あなたのモデルは、1つのModelEmployeeオブジェクトを必要とし、Employeesのリストを与えます。モデルクラスのインスタンスを作成し、そこにリストを入れ、それをビューに渡す必要があります。 –
@SamKuhmonenなので、モデルの新しいインスタンスを作成する必要がありますか?私は初心者です... – NightShade555
コントローラからモデルを返すときには、これを行う必要があります –