我在查看我的数据时遇到问题。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
从控制器返回模型时需要做到这一点 –