问题
I have LabResults table and the order results belongs to departments :
Department number 1 = hematology Department
Department number 2 = Biochemistry Department
Department number 3 = Serology Department
Department number 4 = Hormones Department
When I make new laboratory order and select tests some tests belong to department 1 and some belongs to department 2 and some under department 3 and some under department 4 , and some times the order include tests from one department or 2 department , the point I need in Result view to show the DIV and result if there is tests requested belong to that department for example: When I made order number 5 and tests requested under department 1 , 2 only in the result view it should appear the DIV for department 1 and 2 only and hide DIV 3 and 4. This is the view code and its not working as expected and only show first DIV and hide other div :
@model AljawdahNewSite.Models.Orders_Tables
@{
ViewBag.Title = "CashData";
Layout = "~/Views/Shared/_LayoutPatients.cshtml";
var hema = new List<int>() { 1 };
var bio = new List<int>() { 2 };
var ser = new List<int>() { 3 };
var hor = new List<int>() { 4 };
var culture = new List<int>() { 7 };
var para = new List<int>() { 6,9 };
var labPara = Model.LabParaResult.FirstOrDefault();
var labCult = Model.LabCultureResults.FirstOrDefault();
var labMicro= Model.LabMicroResults.FirstOrDefault();
var labHema = Model.LabResults;
var labBio = Model.LabResults;
var labSer = Model.LabResults;
var labHor = Model.LabResults;
}
@if (labHema != null && labHema.FirstOrDefault().deptid ==1 && labHema.FirstOrDefault().APPROVED_DATE.HasValue)
{
<div class="tab-pane container active" id="home">
<h5 class="text-uppercase p-2 text-center">Hematology Department</h5>
<table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().patient_no)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().RESULT_NUMBER)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(m => m.patients.FirstOrDefault().Patient_Name)</th>
<th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>
</tr>
</thead>
@foreach (var employee in Model.LabResults.Where(x => hema.Contains(x.LabTests.Dept_id.GetValueOrDefault())))
{
<tr>
<td>@employee.patient_no</td>
<td>@employee.RESULT_NUMBER</td>
<td>@employee.APPROVED_DATE</td>
<td>@employee.Patients.Patient_Name</td>
<td>@employee.LabTests.TestName</td>
</tr>
}
</table>
</div>
}
@if (labBio != null && labBio.FirstOrDefault().deptid == 2 && labBio.FirstOrDefault().APPROVED_DATE.HasValue)
{
<div class="tab-pane container active" id="home">
<h5 class="text-uppercase p-2 text-center">Biochemistry Department</h5>
<table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().patient_no)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().RESULT_NUMBER)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(m => m.patients.FirstOrDefault().Patient_Name)</th>
<th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>
</tr>
</thead>
@foreach (var employee in Model.LabResults.Where(x => bio.Contains(x.LabTests.Dept_id.GetValueOrDefault())))
{
<tr>
<td>@employee.patient_no</td>
<td>@employee.RESULT_NUMBER</td>
<td>@employee.APPROVED_DATE</td>
<td>@employee.Patients.Patient_Name</td>
<td>@employee.LabTests.TestName</td>
</tr>
}
</table>
</div>
}
@if (labSer != null && labSer.FirstOrDefault().deptid == 3 && labSer.FirstOrDefault().APPROVED_DATE.HasValue)
{
<div class="tab-pane container active" id="home">
<h5 class="text-uppercase p-2 text-center">Serology Department</h5>
<table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().patient_no)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().RESULT_NUMBER)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(m => m.patients.FirstOrDefault().Patient_Name)</th>
<th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>
</tr>
</thead>
@foreach (var employee in Model.LabResults.Where(x => ser.Contains(x.LabTests.Dept_id.GetValueOrDefault())))
{
<tr>
<td>@employee.patient_no</td>
<td>@employee.RESULT_NUMBER</td>
<td>@employee.APPROVED_DATE</td>
<td>@employee.Patients.Patient_Name</td>
<td>@employee.LabTests.TestName</td>
</tr>
}
</table>
</div>
}
@if (labHor != null && labHor.FirstOrDefault().deptid == 4 && labHor.FirstOrDefault().APPROVED_DATE.HasValue)
{
<div class="tab-pane container active" id="home">
<h5 class="text-uppercase p-2 text-center">Hormones Department</h5>
<table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().patient_no)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().RESULT_NUMBER)</th>
<th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(m => m.patients.FirstOrDefault().Patient_Name)</th>
<th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>
</tr>
</thead>
@foreach (var employee in Model.LabResults.Where(x => hor.Contains(x.LabTests.Dept_id.GetValueOrDefault())))
{
<tr>
<td>@employee.patient_no</td>
<td>@employee.RESULT_NUMBER</td>
<td>@employee.APPROVED_DATE</td>
<td>@employee.Patients.Patient_Name</td>
<td>@employee.LabTests.TestName</td>
</tr>
}
</table>
</div>
}
The view output now shows only first department number div and not show other div if there is results for 2 department only show in view first DIV how to update the view code to show the other div if exists result.
EXAMPLE : Order number 50 has results for department 1 and 2 the view must show the result for deptid 1 and deptid 2 :
@if (labHema != null && labHema.FirstOrDefault().deptid ==1 && labHema.FirstOrDefault().APPROVED_DATE.HasValue) { }
@if (labBio != null && labBio.FirstOrDefault().deptid == 2 && labBio.FirstOrDefault().APPROVED_DATE.HasValue) { }
But its show only first one deptid==1 .and not show second one deptid ==2
What is the error with the condition why not show both div maybe FirstOrDefault() the reason ?
回答1:
You could use the following steeps to do this in the right way :
1 - Use just one variable for Model.LabResults;.
Instead :
var labHema = Model.LabResults;
var labBio = Model.LabResults;
var labSer = Model.LabResults;
var labHor = Model.LabResults;
Use :
var labResults = Model.LabResults;
2 - For each department, use variables and take the first element by department not just the first element, like:
var labHema = labResults.FirstOrDefault(x => x.deptid == 1);
var labBio = labResults.FirstOrDefault(x => x.deptid == 2);
var labSer = labResults.FirstOrDefault(x => x.deptid == 3);
var labHor = labResults.FirstOrDefault(x => x.deptid == 4);
3 - For each DIV use above variables like :
@if(labHema != null && labHema.APPROVED_DATE.HasValue)
.....
@if (labBio != null && labBio.APPROVED_DATE.HasValue)
.....
@if (labSer != null && labSer..APPROVED_DATE.HasValue)
.....
@if (labHor != null && labHor.APPROVED_DATE.HasValue)
4 - You don't need to declare collections for this variables, use just ints like :
var hema = 1;
var bio = 2;
var ser = 3;
var hor = 4;
Use this variables for steep 2 like :
var labHema = labResults.FirstOrDefault(x => x.deptid == hema);
....
AND for each loop in the each DIV like:
@foreach (var employee in Model.LabResults.Where(x => hema == x.LabTests.Dept_id.GetValueOrDefault()))
.....
I hope you find this helpful.
来源:https://stackoverflow.com/questions/62167490/why-only-first-div-appeared-how-to-show-other-div-if-order-available