I have repeatedly run in to a problem when creating ASP.NET MVC 3 controllers to which I haven’t found a good solution/design. Typically I want to create a search page, so I create my controller:
public class MyEntityController: Controller {
public ActionResult Search() {
return View();
}
}
This lets me display the view where the user enters the search parameters. When submitting the search, I would normally have created an action to receive the search parameters and perform the actual search:
[HttpPost]
public ActionResult Search(SearchEditModel model) {
IList<IMyEntity> results = ...; // Do the actual search.
SearchResultsViewModel resModel = ...; // Convert it into a view model.
// NOW WHAT?
}
The question is, what do I do now? I need to send my search results to a view to display them. But I can’t just call return View(resModel); since the Search view is already used to let the user input search parameters. This doesn’t seem like a very uncommon scenario so I’m guessing there is a “proper way” of doing this.
David,
I don’t actually see a problem in approaching the problem in a similar way to how you have now. However, I’d use a viewmodel that included both your search IList as well as the advanced ‘parameters’ for the search (i.e. the SearchEditModel ). this way, you could return the search results in the lower portion of the view, with the search parameters still being visible in the upper portion. this would give the additonal benefit of allowing you to further refine your search if required. You would of course have to amend the signatures of both the httpget and httppost actions to accomodate this.
your mileage may vary of course…