简体   繁体   中英

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type System.Collections.Generic.IEnumerable'

How to solve this error?

The model item passed into the dictionary is of type 'System.Data.EnumerableRowCollection 1[System.Data.DataRow]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[AljawdahNewSite.Models.LAB_INVOICE_VIEW]'.

1- This is the Model:

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- This is the controller:

 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- This is the view:

@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- this is the actionlink when click the link:

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

what I need to change in the code?

I changed the way and used the dbcontext as the follwing code its solved my issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM