I am trying to modify my search on a controller so that it is able to count the number of matching results typed by the user in a search string.So that a user would search for a movie name and there was 1 matching film a message would say “1 movie matched your search criteria: 'terminator'”.
So far this is my code , I have no errors and it searches the data fine but doesnt count the no of results.If anybody knows what I need to do in order to get the search to display a message then that would be very helpfull.The search facility works but I need to count how how many matches there are to the data & display a message if there are no results or if there are and how many.
Thanks
public ViewResult Index(string sortOrder, string searchString)
{
ViewBag.NamesSortParm = string.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
ViewBag.MoviesSortParm = sortOrder == "MovieID" ? "Name" : "Director";
var Movies = from s in MoviesRepository.GetMovies ()
select s;
if (!string.IsNullOrEmpty(searchString))
{
Movies = Movies.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())
|| s.Director.ToUpper().Contains(searchString.ToUpper()));
int count = Movies.Count();
if (count == 0)
{
ViewBag.Message = "No matches found";
}
else
{
ViewBag.Message = " Results";
} }
switch (sortOrder)
{
case "Name":
Movies = Shows.OrderByDescending(s => s.Name);
break;
case "Director":
Movies = Movies.OrderByDescending(s => s.Director);
break;
default:
Movies = Movies.OrderBy(s => s.Synopsis);
break;
}
return View(Movies.ToList());
}