传入字典的模型项的类型是 .. 但此字典需要 System.Collections.Generic.IEnumerable' 类型的模型项

Posted

技术标签:

【中文标题】传入字典的模型项的类型是 .. 但此字典需要 System.Collections.Generic.IEnumerable\' 类型的模型项【英文标题】:The model item passed into the dictionary is of type .. but this dictionary requires a model item of type System.Collections.Generic.IEnumerable'传入字典的模型项的类型是 .. 但此字典需要 System.Collections.Generic.IEnumerable' 类型的模型项 【发布时间】:2022-01-16 00:03:45 【问题描述】:

如何解决这个错误?

传入字典的模型项的类型为“System.Data.EnumerableRowCollection1[System.Data.DataRow]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[AljawdahNewSite.Models.LAB_INVOICE_VIEW]”。

1- 这是模型:

public partial class LAB_INVOICE_VIEW
    
        public int patient_no  get; set; 
        public int order_id  get; set; 

        public string patient_name  get; set; 
        public int testid  get; set; 
        public string testname  get; set; 
        public string order_vat  get; set; 

        public Nullable<decimal> total_amount  get; set; 
        public string status_name  get; set; 
        public Nullable<System.DateTime> COLLECTION_DATE  get; set; 
     
        public Nullable<System.DateTime> RECEIVING_DATE  get; set; 
        
    

2- 这是控制器:

 public ActionResult Index(int id)
        
          
            string sql = @"select  patient_no , 
                                   order_id , 
                                   patient_name ,
                                   testid ,
                                   testname ,
                                   order_vat ,
                                   total_amount ,
                                   status_name ,
                                   COLLECTION_DATE ,
                                   RECEIVING_DATE       
                                   FROM lab_invoice_view
                                   where ORDER_ID = '0' ";

            DataTable dt = func.fireDatatable(string.Format(sql, id));
            LAB_INVOICE_VIEW invoice = new LAB_INVOICE_VIEW();

            foreach (DataRow dr in dt.Rows)
            
                invoice.patient_no = int.Parse(dr["patient_no"].ToString());
                invoice.order_id = int.Parse(dr["order_id"].ToString());
                invoice.patient_name = dr["patient_name"].ToString();
                invoice.testid = int.Parse(dr["testid"].ToString());
                invoice.testname = dr["testname"].ToString();
                invoice.order_vat = dr["order_vat"].ToString();
                invoice.total_amount = decimal.Parse(dr["total_amount"].ToString());
                invoice.status_name = dr["status_name"].ToString();
                invoice.COLLECTION_DATE = DateTime.Parse(dr["COLLECTION_DATE"].ToString());
                invoice.RECEIVING_DATE = DateTime.Parse(dr["RECEIVING_DATE"].ToString());
            


            return View(dt.AsEnumerable());
        

3- 这是视图:

@model IEnumerable<AljawdahNewSite.Models.LAB_INVOICE_VIEW>

@
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutMain.cshtml";


<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.patient_no)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.order_id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.patient_name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.testid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.testname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.order_vat)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.total_amount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.status_name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.COLLECTION_DATE)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RECEIVING_DATE)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) 
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.patient_no)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.order_id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.patient_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.testid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.testname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.order_vat)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.total_amount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.status_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.COLLECTION_DATE)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RECEIVING_DATE)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new  /* id=item.PrimaryKey */ ) |
            @Html.ActionLink("Details", "Details", new  /* id=item.PrimaryKey */ ) |
            @Html.ActionLink("Delete", "Delete", new  /* id=item.PrimaryKey */ )
        </td>
    </tr>


</table>

4- 这是点击链接时的操作链接:

<td>@Html.ActionLink("E-Invoice", "Index", "Invoice", new  id = item.LabOrders.ORDER_ID, new  @class = "btn btn-primary", target = "_blank" )</td>

我需要在代码中更改什么?

【问题讨论】:

所以你正在返回 DataTable 并期待它神奇地变成 IEnumerable&lt;AljawdahNewSite.Models.LAB_INVOICE_VIEW&gt; @Selvin 没想到怎么做 :) 【参考方案1】:

我改变了方式并使用 dbcontext 作为以下代码,它解决了我的问题:

 public ActionResult Index(int id)
        
            var Invoice = db.LAB_INVOICE_VIEW.Where(c => c.order_id == id);
            return View(Invoice);
        

【讨论】:

以上是关于传入字典的模型项的类型是 .. 但此字典需要 System.Collections.Generic.IEnumerable' 类型的模型项的主要内容,如果未能解决你的问题,请参考以下文章

传递到字典中的模型项的类型为'System.Collections.Generic.List`1 []',但此字典需要[duplicate]类型的模型项

MVC4不正确的模型错误

在 ASP.NET MVC 中,使用正确的类型化对象呈现用户控件时遇到类型错误

如何在服务器上解决“传入字典的模型是字符串类型但需要对象”,本地主机没问题

关于 .Net 中通用字典中的引用类型键的快速问题

函数——此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典, 此字典的键值对为此列表的索引及对应元素。例如传入的列表为:[11,22,33]返回的字典为{0:11,