我在查看数据时遇到问题。
我想在我的页面中实现的是,我将所有这些内容放在1个视图中以查看数据,并使用bootstrap进行CRUD操作。
我收到了这种错误:
另外,有人建议我应该使用new(),因为它声明我应该创建一个新实例,但我不知道在哪里放置它。我试图把它放在我的模型中,如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>
&#13;
控制器:
[HttpGet]
public async Task<IActionResult> Index()
{
var getAllEmployees = await _Context.Employee.ToListAsync();
return View(getAllEmployees);
}
答案 0 :(得分:2)
创建一个ViewModel:
public class EmployeeViewModel
{
public List<Employee> Employees { get; set; }
}
您的ViewModel必须是:
@model EmployeeViewModel
在您的操作中,您需要创建ViewModel并将其传递给视图,如:
[HttpGet]
public async Task<IActionResult> Index()
{
var AllEmployees = await _Context.Employee.ToListAsync();
var model = new EmployeeViewModel();
model.Employees = AllEmployees;
return View(model);
}
还要编辑视图以匹配新的ViewModel:
@foreach (var item in Model.Employees)
{
<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>
}