问题
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 ?
回答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.
来源:https://stackoverflow.com/questions/62069443/the-model-item-passed-into-the-dictionary-is-of-type-how-i-will-pass-orders-ta