I have a website that has PageContent, News, Events etc and I have a controller that will handle the search.
In that controller action method I guess I do a var results = SearchClass.Search(searchstring)
to keep the logic out of the controller.
However because I am returning different results because I am searching News, Events etc how do I return the results as they are different models. Do I use a ViewModel and then pass that to the view? return View(SearchModel);
UPDATE: I knocked this up, what do you think:
public ActionResult Search(string criteria)
{
var x = WebsiteSearch.Search(criteria);
return View(x);
}
public static class WebsiteSearch
{
public static SearchViewModel Search(string SearchCriteria)
{
return new SearchViewModel(SearchCriteria);
}
}
public class SearchViewModel
{
private string searchCriteria = String.Empty;
public IEnumerable<News> NewsItems
{
get { return from s in News.All() where s.Description.Contains(searchCriteria) || s.Summary.Contains(searchCriteria) select s; }
}
public IEnumerable<Event> EventItems
{
get { return from s in Event.All() where s.Description.Contains(searchCriteria) || s.Summary.Contains(searchCriteria) select s; }
}
public SearchViewModel(string SearchCriteria)
{
searchCriteria = SearchCriteria;
}
}