我在查看我的數據時遇到問題。ASP.NET Core - 查看數據錯誤
我想在我的頁面中實現的是,我將它們全部放在1個視圖中以查看數據,並使用bootstrap進行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);
}
錯誤非常明顯:您的模型表示需要一個ModelEmployee對象,您爲其提供一個Employees列表。您需要創建模型類的實例,將其放入列表並將其提供給視圖。 –
@SamiKuhmonen,所以我需要創建一個新的模型實例嗎?像這樣:'@model modelEmp = new MoelEmployee();'?,對不起,我是新手... – NightShade555
從控制器返回模型時需要做到這一點 –