1

Why I got this error:

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

This is the model Orders_Tables :

public class Orders_Tables
    {
        public List<Lab_Orders>  LabOrders { get; set; }
        public List<Lab_orders_Cash> LabOrdersCash { get; set; }
        public List<Lab_Sample_status> LabOrderStatus { get; set; }

        public List<LAB_RESULTS> LabResults { get; set; }
        public List<LabTests> labtests { get; set; }
        public List<LAB_RESULTS_CLINIC_VIEW> labViewResult { get; set; }
        public List<LAB_RESULT_CASH_VIEW> labCashView { get; set; }

        public List<LAB_PARASITOLOGY_VIEW> labparaview { get; set; }

       public List<Lab_Hematology_Samples> LabSamples { get; set; }

      public List<Patients> patients { get; set; }

        public Orders_Tables()
        {
            this.LabOrders = new List<Lab_Orders>();
            this.LabOrdersCash = new List<Lab_orders_Cash>();
            this.LabOrderStatus = new List<Lab_Sample_status>();
            this.LabResults = new List<LAB_RESULTS>();
            this.labtests = new List<LabTests>();
            this.labViewResult = new List<LAB_RESULTS_CLINIC_VIEW>();
            this.labCashView = new List<LAB_RESULT_CASH_VIEW>();
            this.labparaview = new List<LAB_PARASITOLOGY_VIEW>();
            this.LabSamples = new List<Lab_Hematology_Samples>();
            this.patients = new List<Patients>();
        }


    }

This is the controller:

public ActionResult ordersCash1()
        {
            Orders_Tables tables = new Orders_Tables();

            var OrdersList = from o in tables.LabOrdersCash
                             join st in tables.LabOrderStatus on o.order_status equals st.status_id
                             where o.patient_no == (int)Session["UserpatientNo"]
                             select o;


            return View(OrdersList);
         }

this is the view code:

@model IEnumerable<AljawdahNewSite.Models.Orders_Tables>
@{
    ViewBag.Title = "ordersCash1";
    Layout = "~/Views/Shared/_LayoutPatients.cshtml";
}

<h2>Orders List</h2>

<table class="table">
        <tr>
            <td> Order No. </td>
            <td> order date    </td>
            <td> MRN Patient No  </td>
            <td> Order Status   </td>


        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.LabOrdersCash.First().cash_order_id</td>
                <td>@item.LabOrdersCash.First().order_date</td>
                <td>@item.LabOrdersCash.First().patient_no</td>
                <td>@item.LabOrderStatus.First().status_name</td>
                <td>@Html.ActionLink("Result Details", "Details1", new { id = item.LabOrdersCash.First().cash_order_id })</td>
            </tr>
        }
    </table>

how to solve this error i checked solutions in the site but its different cases ?

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
Abdullah
  • 983
  • 12
  • 26
  • The action `ordersCash1` send collection of `o` that's mean `IEnumerable` so the model must be `@model IEnumerable` not `@model IEnumerable`, try this and let me know. comment `@foreach (var item in Model)` for test. and if it's OK. adapt it to the new model. – Mohammed Sajid May 28 '20 at 17:22
  • with this initialization `Orders_Tables tables = new Orders_Tables();`, `LabOrdersCash` and `LabOrderStatus` will be empty. – Mohammed Sajid May 28 '20 at 17:36
  • @Sajid i think we need to make change in controller , its return empty , can we make like this tables.LabOrdersCash = db.Lab_orders_Cash.Join(Lab_Sample_status) but how to complete the join statement and then return view(tables) ? – Abdullah May 28 '20 at 17:40
  • in the action : ``Orders_Tables tables = new Orders_Tables(); tables.LabOrdersCash = (from o in db.Lab_orders_Cash join st in db.Lab_Sample_status on o.order_status equals st.status_id where o.patient_no == (int)Session["UserpatientNo"] select o).ToList(); return View(tables);`` in the view : ``@model AljawdahNewSite.Models.Orders_Tables``, this will work well. and for the `foreach`, use ``Model.LabOrdersCash`` instead `Model` – Mohammed Sajid May 28 '20 at 17:51
  • @Sajid this error appeared now : Additional information: LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression. – Abdullah May 28 '20 at 18:14
  • Try to put ``(int)Session["UserpatientNo"]`` into variable and use it for the query – Mohammed Sajid May 28 '20 at 18:17
  • @Sajid thank you its working now , but in join i want to read status_name from table Lab_Sample_status @item.LabOrderStatus.First().status_name when i used fin foreach use Model.LabOrdersCash how I will read status_name? its show order status which is in process or completed – Abdullah May 28 '20 at 18:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214849/discussion-between-sajid-and-abdullah). – Mohammed Sajid May 28 '20 at 18:46

1 Answers1

1

In the action, try to select list of oreders Cach and sample Status and add them to tables object like the following code :

public ActionResult ordersCash1()
{
    Orders_Tables tables = new Orders_Tables();
    int patientId = (int)Session["UserpatientNo"];

    var result  = (from o in db.Lab_orders_Cash
                 join st in db.Lab_Sample_status on o.order_status equals st.status_id
                 where o.patient_no == patientId
                 select new {orederCach = o, sampleStatus = st}).ToList();

    tables.LabOrdersCash = result.Select(x => x.orederCach).ToList();
    tables.LabOrderStatus = result.Select(x => x.sampleStatus).ToList();

    return View(tables);
 }

` In the view, change the model to :

@model AljawdahNewSite.Models.Orders_Tables

You don't need to loop, if you search just the first element for LabOrdersCash and LabOrderStatus

....
    <tr>
        <td>@Model.LabOrdersCash.First().cash_order_id</td>
        <td>@Model.LabOrdersCash.First().order_date</td>
        <td>@Model.LabOrdersCash.First().patient_no</td>
        <td>@Model.LabOrderStatus.First().status_name</td>
        <td>@Html.ActionLink("Result Details", "Details1", new { id = item.LabOrdersCash.First().cash_order_id })</td>
    </tr>

I hope you find this helpful.

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
  • @Abdullah i'm adding answer, i hope this help you. if you have any question, we can continue in the room. – Mohammed Sajid May 28 '20 at 18:48
  • i need the loop because the doctors and patients after login they need to see all laboratory results , i did this orders_tables class because i need to join tables and read results depends on lab department and they need the results for all departments to appear in one page for this reason i need to show every thing in one view , thank you for help :) @Sajid – Abdullah May 28 '20 at 19:14
  • can you please check this issue : https://stackoverflow.com/questions/62086222/how-to-create-multiple-tables-from-one-module-in-mvc-view?noredirect=1#comment109809499_62086222 @Sajid – Abdullah May 29 '20 at 14:15
  • @Abdullah sure, i will check and tell you. – Mohammed Sajid May 29 '20 at 14:25
  • Assalam alikum , I think i found the way by using .Include , can you check this error please when you have time , https://stackoverflow.com/questions/62106805/a-specified-include-path-is-not-valid-the-entitytype-db-a50b96-aljawdahlabmode – Abdullah May 30 '20 at 18:22