我試着在我的視圖中顯示我的客戶列表我得到了這個錯誤傳入字典是'System.Collections.Generic.List`類型,但此字典需要'System.Collections.Generic.IEnumerable`
傳遞到字典的模型項的類型爲「System.Collections.Generic.List
1[PDF.View_model.Customers]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
在填補我在模型層列表,並通過它扔pdf.view_model.View_model這是
public IEnumerable<Customers> GetAllCustomers(int? _id)
{
Ref_customers = new List<Customers>();
CList = new List<Models.EF_Model.Customer>();
foreach (var item in CList)
{
Ref_customers.Add(new Customers() { id = item.Id, FName = item.FName, LName = item.LName, Paid = item.Paid });
}
return Ref_customers;
,並在我的模型
` public List<EF_Model.Customer> GetAllCustmers(int? _id)
{
using (var Context = new EF_Model.CoolerEntities())
{
return (_id.HasValue ? Context.Customers.Where(p => p.Id == _id).ToList() : Context.Customers.ToList());
}
}`
在我的控制器
`
public ActionResult Index(int? id)
{
Ref_ViewModel = new View_model.View_Model();
return View(Ref_ViewModel.GetAllCustomers(id));
}
`
,並在我看來
,最後我貼的是少量的,它顯示出客戶的名單以foreach語句結尾
`
@model IEnumerable<PDF.View_model.Customers>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FName)
</th>
<th>
@Html.DisplayNameFor(model => model.LName)
</th>
<th>
@Html.DisplayNameFor(model => model.Paid)
</th>
<th>
@Html.DisplayNameFor(model => model.Password)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Paid)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
model.custom呃簡直就是我的表的DTO我也有一個在我的view_model.Customer
我如何轉換列表的IEnumerable?我知道名單是IEnumrable只是不知道該怎麼辦
我是困惑
錯誤消息的最後一部分是缺失 - _...類型的 'System.Collections.Generic.IEnumerable _ –
@StephenMuecke IEnumerable的1 [PDF.Models.EF_Model.Customer]'。 –
您有2個客戶類,PDF.View_model.Customers和PDF.Models.EF_Model.Customer? – sachin