1

I got this error when click on details link :

CS1061: 'IEnumerable' does not contain a definition for 'labViewResult' and no extension method 'labViewResult' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

This is my model :

public partial class LAB_RESULTS_CLINIC_VIEW
    {
        public int order_number { get; set; }
        public int Test_Id { get; set; }
        public string Test_Name { get; set; }
        public string Normal { get; set; }
        public Nullable<System.DateTime> Report_Date { get; set; }
        public string Result { get; set; }
        public string Notes { get; set; }
        public string Low_Range { get; set; }
        public string High_Range { get; set; }
        public string Panic { get; set; }
        public string Text_Range { get; set; }
        public string machine_name { get; set; }
        public Nullable<int> Customer_No { get; set; }
        public string Customer_Name { get; set; }
        public Nullable<int> Patient_No { get; set; }
        public string Patient_Name { get; set; }
        public string Clinic_File_No { get; set; }
        public string Category { get; set; }
        public Nullable<int> AGE { get; set; }
        public string SEX { get; set; }
        public string Test_Note { get; set; }
        public string UNIT { get; set; }
        public Nullable<int> DEPTID { get; set; }
        public Nullable<System.DateTime> Collection_Date { get; set; }
        public Nullable<System.DateTime> Receiving_Date { get; set; }
        public string Group_Name { get; set; }
        public Nullable<int> SERIAL { get; set; }
        public int GROUPID { get; set; }
        public Nullable<int> packageid { get; set; }
        public string EXAMINED_BY { get; set; }
        public string APPROVED_BY { get; set; }
        public string UPDATED_BY { get; set; }
        public Nullable<System.DateTime> UPDATED_DATE { get; set; }
        public string Comments { get; set; }
        public string department_name { get; set; }
    }
}

and i created a tables class orders_tables :

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

        public LAB_RESULTS LabResults { get; set; }
        public LabTests labtests { get; set; }
        public LAB_RESULTS_CLINIC_VIEW labViewResult { get; set; }

       public Lab_Hematology_Samples LabSamples { get; set; }

      public Patients patients { get; set; }
    }

Then this is my controller :

public ActionResult MasterDetails(int id)
        {

            List<LAB_RESULTS_CLINIC_VIEW> Lab_View = db.LAB_RESULTS_CLINIC_VIEW.ToList();

            var OrderResult = from o in Lab_View
                              where o.order_number == id
                              select new Orders_Tables { labViewResult = o };

            return View(OrderResult);

        }

Finally this is my view :

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

<h2>Order Result - نتائج التحاليل </h2>

<div style="margin-left:20px">
    <hr/>
    <dl class="horizontal">
        <dt>@Html.DisplayNameFor(model => model.labViewResult.Patient_Name)</dt>
        <dd>@Html.DisplayFor(model => model.labViewResult.Patient_Name)</dd>
        <dt>@Html.DisplayNameFor(model => model.labViewResult.Patient_No)</dt>
        <dd>@Html.DisplayFor(model => model.labViewResult.Patient_No)</dd>
        <dt>@Html.DisplayNameFor(model => model.labViewResult.SEX)</dt>
        <dd>@Html.DisplayFor(model => model.labViewResult.SEX)</dd>
        <dt>@Html.DisplayNameFor(model => model.labViewResult.AGE)</dt>
        <dd>@Html.DisplayFor(model => model.labViewResult.AGE)</dd>
        <dt>@Html.DisplayNameFor(model => model.labViewResult.order_number)</dt>
        <dd>@Html.DisplayFor(model => model.labViewResult.order_number)</dd>


    </dl>
</div>

<div style="align-content:center">
    <table class="table table-bordered">
        <tr>

            <td> test name    </td>
            <td> result  </td>
            <td> From Range   </td>
            <td> To Range </td>
            <td> Other Range    </td>
            <td> Report Date  </td>


        </tr>

        @foreach (var item in Model)
        {
            <tr>

                <td>@item.labViewResult.Test_Name</td>
                <td>@item.labViewResult.Result</td>
                <td>@item.labViewResult.Low_Range</td>
                <td>@item.labViewResult.High_Range</td>
                <td>@item.labViewResult.Text_Range</td>
                <td>@item.labViewResult.Report_Date</td>


            </tr>
        }
    </table>
</div>

The error appeared in displatyfor rows

@Html.DisplayFor(model => model.labViewResult.Patient_Name)

How to solve this error , I checked solutions in this site but not solved .

Abdullah
  • 983
  • 12
  • 26
  • 1
    The model is `IEnumerable`, and `labViewResult` not exist in `IEnumerable`, so i suggest to use `model.First().labViewResult...` – Mohammed Sajid May 26 '20 at 00:45
  • @Sajid Thank you sooooo much its working , you can add answer , another thing its coming the title in one row and data in another row how i will put it in one row title and data
    how to concatenate in one row , thank you again :)
    – Abdullah May 26 '20 at 01:43
  • @Sajid assalam alykum brother how are you can you please help me in print crystal reports in MVC this is the question https://stackoverflow.com/questions/62498523/how-to-transfer-this-code-from-asp-net-to-asp-net-mvc-to-print-crystal-reports?noredirect=1#comment110528249_62498523 – Abdullah Jun 21 '20 at 18:33
  • salam brother, i'm never used Crustal report, i checked this online, may be will help [link1](https://www.c-sharpcorner.com/article/using-crystal-report-with-asp-net-mvc-5/),[link2](https://www.c-sharpcorner.com/article/use-crystal-report-in-mvc-net/), [link3](https://www.aspsnippets.com/Articles/Implementing-Crystal-Reports-in-ASPNet-MVC.aspx) – Mohammed Sajid Jun 21 '20 at 19:29
  • @Sajid Thank you brother the crystal reports now running , my issue now how to pass multiple parameters from MVC views to webform , i give static values for order_id , dept_Id , test_id and culture and its printing – Abdullah Jun 21 '20 at 20:35

1 Answers1

1

You can solve it by using FirstOrDefault() or First() only but with First() maybe if the order empty will get error "Sequence contains no elements" so the recommended to use FirstOrDefault() :

<dt>@Html.DisplayNameFor(model => model.labViewResult.Patient_Name)</dt>
<dd>@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.Patient_Name)</dd>
<dt>@Html.DisplayNameFor(model => model.labViewResult.Patient_No)</dt>
<dd>@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.Patient_No)</dd>
<dt>@Html.DisplayNameFor(model => model.labViewResult.SEX)</dt>
<dd>@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.SEX)</dd>
<dt>@Html.DisplayNameFor(model => model.labViewResult.AGE)</dt>
<dd>@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.AGE)</dd>
<dt>@Html.DisplayNameFor(model => model.labViewResult.order_number)</dt>
<dd>@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.order_number

And about your question to concatenate dt and dd you can apply some style to dt and dd , you can try the following :

<dt style="width: 30%;display: inline-block;">@Html.DisplayNameFor(model => model.labViewResult.Patient_Name)
</dt><dd style="width: 40%;display: inline-block;">@Html.DisplayFor(model => model.FirstOrDefault().labViewResult.Patient_Name)</dd>
  • 1
    If the list is empty the ``FirstOrDefault`` will not resolve the problem, because ``labViewResult`` will be null. So ``DisplayFor`` will throw *null reference exception*, you must put it in variable and use it with test of null just for ``DisplayFor`` – Mohammed Sajid May 26 '20 at 17:38