【问题标题】:The model item passed into the dictionary is of type , how i will pass orders_tables model with variable传递到字典中的模型项是类型,我将如何传递带有变量的 orders_tables 模型
【发布时间】: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&lt;Lab_orders_Cash&gt; 所以模型必须是@model IEnumerable&lt;AljawdahNewSite.Models.Lab_orders_Cash&gt; 而不是@model IEnumerable&lt;AljawdahNewSite.Models.Orders_Tables&gt;,试试这个,让我知道。评论@foreach (var item in Model) 进行测试。如果没关系。使其适应新模型。
  • 使用此初始化Orders_Tables tables = new Orders_Tables();LabOrdersCashLabOrderStatus 将为空。
  • @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


【解决方案1】:

在操作中,尝试选择 orderers Cachsample Status 列表并将它们添加到tables 对象,如下代码:

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);
 }

` 在视图中,将模型更改为:

@model AljawdahNewSite.Models.Orders_Tables

如果您只搜索LabOrdersCashLabOrderStatus 的第一个元素,则无需循环

....
    <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>

希望对您有所帮助。

【讨论】:

  • @Abdullah 我正在添加答案,希望对您有所帮助。如果您有任何问题,我们可以继续讨论。
  • 我需要循环,因为医生和患者在登录后他们需要查看所有实验室结果,我做了这个 orders_tables 类,因为我需要加入表格并读取结果取决于实验室部门,他们需要结果出于这个原因,所有部门都出现在一个页面中,我需要在一个视图中显示所有内容,谢谢您的帮助:) @Sajid
  • 你能检查一下这个问题吗:stackoverflow.com/questions/62086222/…@Sajid
  • @Abdullah 当然,我会检查并告诉你。
  • Assalam alikum ,我想我找到了使用 .Include 的方法,你有时间可以检查一下这个错误吗,stackoverflow.com/questions/62106805/…
猜你喜欢
  • 2015-04-20
  • 1970-01-01
  • 2017-04-21
  • 2015-12-19
  • 1970-01-01
  • 2015-09-25
  • 2012-04-24
  • 2019-01-12
  • 1970-01-01
相关资源
最近更新 更多