【发布时间】:2020-05-29 02:51:36
【问题描述】:
为什么会出现这个错误:
传入字典的模型项的类型为“AljawdahNewSite.Models.Orders_Tables”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[AljawdahNewSite.Models.Orders_Tables] 的模型项'。
这是模型 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>();
}
}
这是控制器:
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);
}
这是视图代码:
@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>
如何解决这个错误我检查了网站中的解决方案,但情况不同?
【问题讨论】:
-
动作
ordersCash1发送o的集合意味着IEnumerable<Lab_orders_Cash>所以模型必须是@model IEnumerable<AljawdahNewSite.Models.Lab_orders_Cash>而不是@model IEnumerable<AljawdahNewSite.Models.Orders_Tables>,试试这个,让我知道。评论@foreach (var item in Model)进行测试。如果没关系。使其适应新模型。 -
使用此初始化
Orders_Tables tables = new Orders_Tables();、LabOrdersCash和LabOrderStatus将为空。 -
@Sajid 我认为我们需要在控制器中进行更改,它的返回为空,我们可以像这样创建 tables.LabOrdersCash = db.Lab_orders_Cash.Join(Lab_Sample_status) 但是如何完成连接语句然后返回视图(表格)?
-
在操作中:
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);在视图中:@model AljawdahNewSite.Models.Orders_Tables,这会很好用。对于foreach,使用Model.LabOrdersCash而不是Model -
@Sajid 现在出现此错误:附加信息:LINQ to Entities 无法识别方法“System.Object get_Item(System.String)”方法,并且此方法无法转换为存储表达式。
标签: asp.net-mvc model-view-controller