Heres my Code and it works fine. I just dont like all the If else, what If I keep adding field to filter, its going to get messy. Ps: I am not using EF and cannot on this. Thanks.
CONTROLLER CODE
var books = bookRepository.GetDogs();
if (!String.IsNullOrEmpty(searchString))
{
if (!String.IsNullOrWhiteSpace(searchString) && searchGender != String.Empty && searchHand != String.Empty)
{
books = books.Where(b => b.Name.ToUpper().Contains(searchString.ToUpper())
&& b.Gender == searchGender && b.Handedness == searchHand);
}
else if (!String.IsNullOrWhiteSpace(searchGender) && searchString == String.Empty && searchHand == String.Empty)
{
books = books.Where(b => b.Gender == searchGender);
}
else if (String.IsNullOrWhiteSpace(searchGender) && searchString != String.Empty && searchHand == String.Empty)
{
books = books.Where(b => b.Name.ToUpper().Contains(searchString.ToUpper()));
}
}
return View(books);
HERES MY VIEW
<p>Find by Name : @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
@Html.DropDownList("searchGender", new SelectList(ViewBag.BookNames), "-- Select All --")
@Html.DropDownList("searchHand", new SelectList(ViewBag.HandNames), "---Select All--")
var books = from b in bookRepository.GetDogs() select b;equivalent tobookRepository.GetDogs()? – George Mauer Oct 6 '12 at 20:38