// GET: PatientInfoes public ActionResult Index() { IEnumerable <PatientInfo> PatientList; if (!string.IsNullOrEmpty(Request.Unvalidated["searchItem"])) { string searchItem = Request.Unvalidated["searchItem"]; int Labno = -1; if (int.TryParse(searchItem, out Labno)) //searchItem is lab no { if (db.PatientInfo.Where(x => x.LabNo == Labno).SingleOrDefault() != null) { PatientList = db.PatientInfo.Where(x => x.LabNo == Labno); PatientList = PatientList.OrderByDescending(x => x.CreatedOn.Date).ThenByDescending(x => x.CreatedOn.TimeOfDay); return(View(PatientList)); } else { ViewBag.message = "No record found. Click to go back"; return(View()); } } else if (searchItem.All(c => Char.IsLetter(c) || c == ' ')) //searchItem is name { PatientList = db.PatientInfo.Where(x => x.Name.Contains(searchItem)); PatientList = PatientList.OrderByDescending(x => x.CreatedOn.Date).ThenByDescending(x => x.CreatedOn.TimeOfDay); if (PatientList != null && PatientList.Any() == true) { return(View(PatientList)); } else { ViewBag.message = "No record found. Click to go back"; return(View()); } } else //searchItem is bad { ViewBag.message = "No record found. Click to go back"; return(View()); } } PatientList = db.PatientInfo; PatientList = PatientList.OrderByDescending(x => x.CreatedOn.Date).ThenByDescending(x => x.CreatedOn.TimeOfDay); return(View(PatientList)); }