2017-04-02 74 views
1

我在查看我的数据时遇到问题。ASP.NET Core - 查看数据错误

我想在我的页面中实现的是,我将它们全部放在1个视图中以查看数据,并使用bootstrap进行CRUD操作。

我收到了这样那样的错误:

image

此外,有人建议我应该用新的(),因为它说,我应该创建一个新的实例,但我不知道在哪里放好它。我试图把它放在我的模型中,如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); 
} 
+0

错误非常明显:您的模型表示需要一个ModelEmployee对象,您为其提供一个Employees列表。您需要创建模型类的实例,将其放入列表并将其提供给视图。 –

+0

@SamiKuhmonen,所以我需要创建一个新的模型实例吗?像这样:'@model modelEmp = new MoelEmployee();'?,对不起,我是新手... – NightShade555

+1

从控制器返回模型时需要做到这一点 –

回答

2

创建一个视图模型:

public class EmployeeViewModel 
{ 
    public List<Employee> Employees { get; set; } 
} 

您的视图模型将是:

@model EmployeeViewModel 

在你的行动,你需要创建视图模型,并把它传递给像视图:

[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> 

} 
+0

Hi @Cristian,你可以看到我把所有ModelEmployee所有必要的模型,我将在单个视图中使用。您发布的代码对我无效,您是否有任何其他想法解决此问题?似乎我并不真正了解MVC的概念,但我仍然在学习ASP.NET。 – NightShade555

+0

修改我的答案。现在应该为你工作。 –