Ganesh UP
Ganesh UP

Reputation: 107

Searching & sorting with AJAX pagination in MVC4

I am developing MVC4 application with Searching, Paging and Sorting. Everything works fine with normal view. Now I am converting same application using Ajax & Partial View. How can I pass my search & sort parameter through Paging control residing on partial View. Now, I am able to load filtered data in partial view but moving to next page, lost my search parameters and loads all records ignoring search filters.

My Paging Control in _PartialView.cshtml

@Html.PagedListPager(Model,
                        page => Url.Action("AjaxMethod", 
                            new 
                            {  
                                page,
                                searchByUserName = Request.QueryString["searchByUserName"],
                                searchByReaderName = Request.QueryString["searchByReaderName"],
                                searchByReaderType = Request.QueryString["searchByReaderType"],
                                searchByUploadDate = Request.QueryString["searchByUploadDate"],
                                sortBy = Request.QueryString["sortBy"]
                            }), 
                        //new PagedListRenderOptions.() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true }
                        PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions(){ HttpMethod = "GET", UpdateTargetId = "divData"})
                        )

Please suggest, what I am missing....

Upvotes: 0

Views: 716

Answers (1)

Ganesh UP
Ganesh UP

Reputation: 107

I came to know what I m missing, Partial View doesn't got Request.QueryString values thats why paging control lost search parameters and loads all records. So I modified Pager control in _PartialView.cshtml as follows :

@Html.PagedListPager(Model,
                        page => Url.Action("AjaxMethod",
                            new
                            {
                                page,
                                searchByUserName = ViewBag.searchByUserName, // Request.QueryString["searchByUserName"],
                                searchByReaderName = ViewBag.searchByReaderName, // Request.QueryString["searchByReaderName"],
                                searchByReaderType = ViewBag.searchByReaderType, // Request.QueryString["searchByReaderType"],
                                searchByUploadDate = ViewBag.searchByUploadDate, // Request.QueryString["searchByUploadDate"],
                                sortBy = ViewBag.sortBy // Request.QueryString["sortBy"]
                            }),                        
                        PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() 
                        { HttpMethod = "GET", UpdateTargetId = "divData", LoadingElementId="divloading" })
                        )

These ViewBag objects initialized in controllers action method. Anyone having better solution than this for the above problem. Please suggest.

Upvotes: 0

Related Questions