【问题标题】:passed into the dictionary is of type 'System.Collections.Generic.List`but this dictionary requires'System.Collections.Generic.IEnumerable` [duplicate]传入字典的类型为“System.Collections.Generic.List”,但该字典需要“System.Collections.Generic.IEnumerable”[重复]
【发布时间】:2017-10-10 05:44:27
【问题描述】:

我正在尝试在我的视图中显示我的客户列表我收到此错误

传入字典的模型项的类型为'System.Collections.Generic.List1[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.customer 只是我的表的 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?
  • 它们是不同的类型。更改视图以匹配您传递的模型 - @model IEnumerable&lt;PDF.View_model.Customers&gt;
  • 在您的视图中,您使用的是 EF_Model.Customer,但您似乎从 Controller 传递了另一种类型。

标签: c# asp.net-mvc runtime-error


【解决方案1】:

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

要么您需要更新您的视图以使用其他类型PDF.View_model.Customers,或者让控制器通过ViewModel 类型PDF.Models.EF_Model.Customer

【讨论】:

    【解决方案2】:

    您的列表确实是IEnumerable&lt;PDF.View_model.Customers&gt;,但您需要的是IEnumerable&lt;PDF.Models.EF_Model.Customer&gt;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-25
      • 2013-10-17
      • 1970-01-01
      • 2020-07-21
      • 2022-01-16
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多