I have the following action method that builds a drop down list which is used for searching records that have a specific country. In addition to showing a list of all countries, I have added a static value named –Any— which will return all the records regardless of their country, as shown below:
public PartialViewResult ManageVisitSearch()
{ var CountryList = repository.FindAllCountry().ToList();
CountryList.Insert(0, new Country { Description = "--Any--", CountryID = 0 });
//code goes here
ViewBag.CountryID = new SelectList(CountryList, "CountryID", "Description", 0);
return PartialView("_ManageVisitSearch"); }
On the repository method which will perform the search, I did the following to return all the records in case the –Any— was selected from the dropdown lists:
public IEnumerable<VisitSearch> visitsearch(DateTime? datefrom, DateTime? dateto, int countryid, int genderid)
{
var vs = (from v in entities.Visits where
(v.Patient.NationalityID == countryid || countryid == 0)
//code goes here
The above approach is working fine with me, but I think that it might not be the best approach to follow. Is there a more rational approach to follow to add the –Any— to my search dropdown lists, or does my approach sound good?
You can build your query by adding where clauses based on some conditions.