Simply I have 4 textboxes and I want to search and filter data by one or two or 3 or 4 textboxes and execlude the textbox if i didn't want to search by that textbox :
I am using entityFramework , and I have orders_class include the tables Lab_Orders,Lab_Sample_status,Patients:
The fields I have :
Start Date ---------------
End Date -----------------
Polyclinic File No -------------
ID or Passport No --------------
I select the data depends on the value entered in the textbox fields
I want to search some times the orders between 2 dates (start date to end date) only .
Some times I need to search all orders by patient passport number
some times I need to search by polyclinic File No only
1- I created the View input textboxes :
@using (Html.BeginForm("Clinicsorders", "Orders", FormMethod.Get))
{
<span id="FromDate">From Date</span><input id="FromDatetext" type="text" name="startdate"/>
<span id="ToDate"> To Date</span> <input id="ToFatetext" type="text" name="enddate"/>
<span id="PCfileno">Polyclinic File No</span><input id="pcfilenotext" type="text" name="pcfileno" />
<span id="IDNO">ID/Iqama No</span> <input id="idnotext" type="text" name="idno" />
<br/>
<br/>
<input id="Submit1" type="submit" value="Search Orders" style="margin-left:1000px" />
}
2- I created the controller :
public ActionResult Clinicsorders(DateTime? startdate,DateTime? enddate,string pcfileno,string idno)
{
List<Lab_Orders> Clinicorder = db.Lab_Orders.OrderByDescending(x => x.ORDER_ID).ToList();
List<Lab_Sample_status> samplestatus = db.Lab_Sample_status.ToList();
List<Patients> patients = db.Patients.ToList();
if (startdate == null && enddate == null && pcfileno == null && idno == null)
{
var OrdersList = from o in Clinicorder
join st in samplestatus on o.order_status equals st.status_id
join pt in patients on o.patient_no equals pt.Patient_No
where o.CUSTID == (int)Session["UserCustid"]
select new Orders_Tables { LabOrders = o, LabOrderStatus = st , patients = pt };
return View(OrdersList);
}
else if (startdate != null || enddate != null || pcfileno !=null || idno != null)
{
var OrdersList = from o in Clinicorder
join st in samplestatus on o.order_status equals st.status_id
join pt in patients on o.patient_no equals pt.Patient_No
where o.CUSTID == (int)Session["UserCustid"] && (o.ORDER_DATE >= startdate && o.ORDER_DATE <= enddate)
select new Orders_Tables { LabOrders = o, LabOrderStatus = st , patients = pt };
return View(OrdersList);
}
return View();
}
When I run the view All orders selected by default.
Also I can search and filter now successfully between 2 dates (startdate and enddate).
I want now the remaining parts to filter and search by pcfileno and idno which is string value.
How I will implement this functionality and filter only by and search with the data I need and execlude the null textboxes.
In ASP.NET I implemented this filter before like the following code :
protected void BtnSearch_Click(object sender, EventArgs e)
{
if (Session["custid"] != null && Convert.ToInt32(Session["custid"]) > 0)
{
string sql = @"SELECT [LAB_RESULTS].ORDER_ID as 'order number'
,APPROVED_DATE as 'Report Date'
,labtests.TestId as 'Test Id'
,labtests.TestName as 'Test_Name'
,labtests.culture as 'Culture'
,CASE WHEN coalesce([RESULT_NUMBER],'') <> '' THEN RESULT_NUMBER WHEN coalesce(RESULT_REPORT, '') <> '' THEN RESULT_REPORT END AS Result
,LAB_RESULTS.CUSTID as 'Customer No'
,Customers.CustName as 'Customer Name'
,LAB_RESULTS.patient_no as 'Patient No'
,Patients.PATIENT_NAME as 'Patient Name'
,patientcat.CatName as 'Category'
,Patients.Age AS 'AGE'
,gender.gendername as 'SEX'
,LAB_RESULTS.deptid AS 'DEPTID'
,TestsRanges.LowerLimit as 'Low Range'
,TestsRanges.UpperLimit as 'High Range'
,TestsRanges.text_range as 'Text Range'
,Lab_Hematology_Samples.COLLECTION_DATE as 'Collection Date'
,LAB_RESULTS.REQ_FORM_NO as 'REQ FORM NUMBER'
FROM [dbo].[LAB_RESULTS]
inner join LabTests on LabTests.testid=LAB_RESULTS.TESTID
inner join TestsRanges on TestsRanges.TestId = LAB_RESULTS.TESTID
inner join PatientCat on patientcat.CatId = TestsRanges.CatId
inner join Customers on Customers.CustId=LAB_RESULTS.CUSTID
inner join patients on Patients.Patient_No = LAB_RESULTS.patient_no
inner join gender on gender.genderid = Patients.Gender
inner join Lab_Orders on Lab_Orders.ORDER_ID = LAB_RESULTS.ORDER_ID
inner join Lab_Hematology_Samples on Lab_Hematology_Samples.ORDER_ID = LAB_RESULTS.ORDER_ID
where lab_results.deptid =1
and (Lab_Orders.catid = TestsRanges.CatId or PatientCat.CatId =5)
and LAB_RESULTS.EXAMINED_DATE is not null
and LAB_RESULTS.APPROVED_DATE is not null
and LAB_RESULTS.update_count in (select max(update_count) from LAB_RESULTS A where A.SAMPLE_STATUS = 6 and A.deptid=LAB_RESULTS.DEPTID and A.ORDER_ID = LAB_RESULTS.ORDER_ID)
and (Patients.Gender = TestsRanges.PatientSex or TestsRanges.PatientSex = 1 )
AND TestsRanges.machine_id = LAB_RESULTS.machine_id ";
string condition = "";
string orderby = "";
condition += " and LAB_RESULTS.custid ='" + Session["custid"] + "'";
orderby += "order by LAB_RESULTS.ORDER_ID desc";
if (DropDownTest.SelectedValue != "")
{
condition += " and labtests.TestId ='" + DropDownTest.SelectedValue + "'";
}
if (TxtPatientNo.Text != "")
{
condition += " and LAB_RESULTS.patient_no='" + TxtPatientNo.Text + "'";
}
if (txtreq.Text != "")
{
condition += " and LAB_RESULTS.REQ_FORM_NO='" + txtreq.Text + "'";
}
if (TxtFromDate.Text != "")
{
string date_day = Convert.ToDateTime(TxtFromDate.Text).Day.ToString();
string date_month = Convert.ToDateTime(TxtFromDate.Text).Month.ToString();
string date_year = Convert.ToDateTime(TxtFromDate.Text).Year.ToString();
string date = date_month + "/" + date_day + "/" + date_year;
condition += " and Lab_Hematology_Samples.COLLECTION_DATE > '" + date + "'";
}
if (TxtToDate.Text != "")
{
string date_day = Convert.ToDateTime(TxtToDate.Text).Day.ToString();
string date_month = Convert.ToDateTime(TxtToDate.Text).Month.ToString();
string date_year = Convert.ToDateTime(TxtToDate.Text).Year.ToString();
string date = date_month + "/" + date_day + "/" + date_year;
condition += " and Lab_Hematology_Samples.COLLECTION_DATE < '" + date + "'";
}
DataTable dt = func.fireDatatable(string.Format(sql + condition + orderby));
GridView1.DataSource = dt;
GridView1.DataBind();
}
How i will make same functionality in MVC and linq
If pcfileno textbox != null then search
if idno textbox != null then search ??