asp.netsortingpaginationwebgrid

How to persist sorting and paging in ASP.NET MVC WebGrid when updating via ajax?


When using ASP.NET WebGrid paging and sorting works by appending a specific query string to the url which contains names and values stating page number, sorting direction etc. However, when using ajaxUpdateId property to be able to update WebGrid asynchronously, when clicking on pages in footer or clicking on headers to sort a column it works fine, but query string is no more appending to url which is not persisting paging and sorting after refreshing the browser page. Is there a way to persist sorting and paging values while updating WebGrid via ajax? Thanks a lot in advance for any useful suggestions


Solution

  • I think I found solution. Maybe it's not perfect but it works. I spent so much time trying to figure out how WebGrid works when it's updating via ajax, however all that functionality is kind of hidden. So if you need to keep sorting and paging even when you refresh the page you have to update url everytime you change a page or sorting. WebGrid remembers it by query string in url. So I added a jquery click event to all links within the WebGrid and used history.pushState function to save the clicked url and then refreshed the page (you need to do that since it will work only for one change as ajax will not change the url)

      <script type="text/javascript">
        $("#gridContainderID a").click(function () {
            history.pushState(null, null, this.href);
            location.reload();
        });
        </script>