MVC3 Webgrid sorting and paging

Helpful Webgrid walk through: MSDN Magazine
My challenge was that after filtering rendered collection, when clicked any sorting link I was getting full collection back.
Useful info

“When WebGrid renders the paging links, it reuses the page URL and attaches a query string parameter with the page number, such as http://localhost:27617/Product/DefaultPagingAndSorting?page=3”

Inspect with Firebug and you will see what’s passed to the backend.

Paging
webgrid_paging
Sorting
webgrid_sorting

All I had to do was modify Index action adding ‘page‘ and ‘sort‘ parameters.

public ActionResult Index(int page = 0, string sort = null)
{

	if (page == 0 && string.IsNullOrWhiteSpace(sort))
	{
		CurrentListing = new Listing();
	}
	else
	{
		TempData.Keep("CurrentListing");
	}

Leave a Reply

Your email address will not be published. Required fields are marked *