I have read many questions and tutorials about this, I couldn’t find a case similar to mine.
I have an index view which has a search form inside to filter results available in my database.
Here’s the simplified version my controller action for my index view:
[HttpPost]
public ActionResult Index(String carMake, String carModel)
{
var cars = from d in db.Cars
select d;
if (!String.IsNullOrEmpty(carMake))
{
if (!carMake.Equals("All Makes"))
{
cars = cars.Where(x => x.Make == carMake);
}
}
if (!String.IsNullOrEmpty(carModel))
{
if (!carModel.Equals("All Models"))
{
cars = cars.Where(x => x.Model == carModel);
}
}
cars = cars.OrderBy(x => x.Make);
return View("SearchResult", cars);
}
as you can see, I get the data from Index view and post it to my SearchResult view directly without the use of any public ActionResult SearchResult(){...}. But I do have public ActionResult SearchResult(){...} method in my controller.
Now, I’m trying to sort my results. How can I do that?
I have tried many different ways to pass data from my Index method to Search Result method such as TempData and so on. But the problem with them is when I click on the sort button the page reloads and the Query that I have taken from my Index view form will be all null because TempData is only useful for passing data among methods and when the method is recalled, the data will be gone and all null.
I have some solutions in my mind but I’m not sure if they will work out and if they’re the best/easiest solution possible to come up with. Which is recording the Query I get from my Index page into a table (I don’t think would be a wise thing to do tho).
I think it should be as;