【问题标题】: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 没想到怎么做 :)

标签: c# asp.net-mvc model-view-controller


【解决方案1】:

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

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

【讨论】:

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