2017-04-02 8 views
1

データの表示に問題があります。ASP.NETコア - データの表示エラー

私が私のページで達成しようとしているのは、データを見るために1つのビューにすべてを置き、ブートストラップを使って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

エラーは非常に明確です:あなたのモデルは、1つのModelEmployeeオブジェクトを必要とし、Employeesのリストを与えます。モデルクラスのインスタンスを作成し、そこにリストを入れ、それをビューに渡す必要があります。 –

+0

@SamKuhmonenなので、モデルの新しいインスタンスを作成する必要がありますか?私は初心者です... – NightShade555

+1

コントローラからモデルを返すときには、これを行う必要があります –

答えて

2

のViewModelを作成します。

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

あなたのViewModelがなければならないだろう。

[HttpGet] 
public async Task<IActionResult> Index() 
{ 
    var AllEmployees = await _Context.Employee.ToListAsync(); 

    var model = new EmployeeViewModel(); 
    model.Employees = AllEmployees; 

    return View(model); 
} 
:あなたはViewModelにの作成などのビューにそれを渡す必要があり、あなたのアクションで

@model EmployeeViewModel 

新しい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

こんにちは@Cristian、私はすべてを載せることができます。 ModelEmployee私は単一のビュー内で使用する必要なすべてのモデル。投稿したコードが私の上で動作しませんでしたが、これを修正する方法について他のアイデアはありますか?まだ私はASP.NETを勉強しているので、まだMVCのコンセプトを理解していないようです。 – NightShade555

+0

私の答えを変更しました。今あなたのために働くべきです。 –

関連する問題