2017-04-02 43 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

修改我的答案。現在應該爲你工作。 –