Suppose I have a controller that provides search functions for a student:
public class StudentSearchController { [HttpGet] public ActionResult Search(StudentSearchResultModel model) { return View(model); } }
While the search action is provided by StudentSearchResultModel, it will display a list of search results.
Is there a way to effectively extend this action method from another controller? For example, let's say I want to have other controllers that should look for students, for example:
public class UniStudentController { [HttpPost] public ActionResult Search(UniStudentSearchResultModel model) { return RedirectToAction("Search", "StudentSearch", model); } } public class HighSchoolStudentController { [HttpPost] public ActionResult Search(HighSchoolSearchResultModel model) { return RedirectToAction("Search", "StudentSearch", model); } }
(assuming both models extend StudentSearchResultModel.)
I obviously cannot do this because I cannot pass pre-created model classes to the search controller (the original search controller recreates StudentSearchResultModel rather than using the passed model).
The best solution I have come up with so far is moving SearchView.cshtml to the Shared folder, and then I can just visualize the view from Uni / HighSchool controllers directly (instead of calling RedirectToAction). This works fine, and theoretically I would not need StudentSearchController. However, I rely on legacy code here (on this far-fetched example, StudentSearchController is deprecated), therefore, without doing a refactoring boat, the General folder is not an option for me.
Another solution would be to put all the search related actions in StudentSearchController, so it will get two actions for UniStudentSearch and HighSchoolStudentSearch. I do not like this approach, as it means that StudentSearchController should be aware of all of its alleged habits.
Any ideas?
PS: Not against refactoring, but limited by deadlines!