2017-05-11 275 views
1

我试着在我的视图中显示我的客户列表我得到了这个错误传入字典是'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只是不知道该怎么办

我是困惑

+0

错误消息的最后一部分是缺失 - _...类型的 'System.Collections.Generic.IEnumerable _ –

+0

@StephenMuecke IEnumerable的1 [PDF.Models.EF_Model.Customer]'。 –

+0

您有2个客户类,PDF.View_model.Customers和PDF.Models.EF_Model.Customer? – sachin

回答

1

你似乎有2个客户类。您正在传递控制器中的一种类型,并在视图中使用另一种作为Model

要么你需要更新你的视图使用其它类型PDF.View_model.Customers,或使类型PDF.Models.EF_Model.Customer

1

你的名单的确是IEnumerable<PDF.View_model.Customers>的控制器通ViewModel,但你需要的是IEnumerable<PDF.Models.EF_Model.Customer>

这不是List不匹配,它是它的通用参数。

相关问题