Im developing a system using MVC3, and to render Grid, it is necessary to use WebGrid. I have an ActionResult that returns the Index page that contains an empty grid. I have another ActionResult that receive those filter parameters, execute the filter query and returns the grid populated. When I page this grid, the grid returns empty because it called the Index ActionResult.
I found a solution. It envolves keeping your search on Session (or Cache or TempData), and nothing more than that. The solution is good because you can adapt it to your real world situation, doesnt need specific classes JQuery.
The magic trick happens inside your Index ActionResult (or your default ActionResult, that will render the grid page at its default behavor).
Code example:
[HttpGet]
public ActionResult Index()//My default action result that will render the grid at its default situation
{
SearchViewModel model = new SearchViewModel();
if (Request.IsAjaxRequest()) //First trick is here, this verification will tell you that someone sorted or paged the grid.
{
if (Session["SearchViewModel"] != null) //If session is not empty, you will get the last filtred values from it.
model = (SearchViewModel)Session["Filtro"];
}
else // If it is not an AjaxRequest, you have to clear your Session, so new requests to Index with default behavior won't display filtred values.
{
Session["SearchViewModel"] = null;
}
model.GridResult = ExecuteFilter(model); // OPITIONAL! This code dependes on how is your real world situation. Just remember that you need to return a default behavior grid if the request was not called by the WebGrid, or return filtred results if WebGrid requested.
return View(model);
}
So, this will be your default ActionResult. It will verify if the request was called by the WebGrid paging or sorting event, to decide if returns filtred results or normal behavior result.
Next step is the search POST ActionResult:
[HttpPost]
public ActionResult Index(SearchViewModel pesquisa) // IMPORTANT!! It is necessary to be the SAME NAME of your GET ActionResult. The reason for that I know, but won't discuss here because it goes out of the question.
{
SearchViewModel model = new SearchViewModel();
model.GridResult = ExecuteFilter(pesquisa); // Execute your filter
Session["SearchViewModel"] = model; //Save your filter parameters on Session.
return View("Index", model);
}
Thats it. The Index.cshtml doesn't have any trick. Just a SearchForm to the ActionResult Index, passing my SearchViewModel as parameter.
Why this solution works?
Well, when you click to sort or page, the WebGrid execute a JavaScript similar to this:
$('#yourGrid').load('it pass the url used to display your current Page, and some paging or sorting parameters, but those are used by the WebGrid') Since it does a .load() method, the request will be a GET, and will hit your Index GET ActionResult. But it is an AJAX call, so our magic trick will execute the filter again with the parameters you saved on Session.
The unique detail I alert, is about your default grid behavior. The GET Index ActionResult MUST EVER RETURNS a valid grid result, not matter if it has or not filters on Session.