MVC 3 WebGrid Paging doesn't return orginal search results

孤街醉人 提交于 2019-12-12 02:53:55

问题


I have a search page with which on load fills a webgrid. My issue is with paging. On load I fill the grid with "Non published" articles (there are none in this case). Then I search for "Currently Published) and get 3 rows.

Now I have 3 pages (I set paging to one row per page for testing purposes). The first time it searches - it gets the right answers in the grid,a nd lets say there are 2 pages.

If I click the 2 to go to the second page - the grid fills with the criteria of the load - which is Non Published of which there are none. I see that I am not going back to the HttpPost action on the page click - so I am not sure why it does this.

You can see I have an initial load called Admin and a Post called Admin(FormCollection values) On both I set the defaults back to "Not Published"

        public ActionResult Admin()
        {

            var menus =
            (
                 from p in db.Menus.ToList()
                 where p.ParentID == 0
                 orderby p.Order ascending
                 select p.Name
            );
            ViewBag.Menus = new SelectList(menus, "Home");

            string[] publishedStatuses = new string[3] 
                   { "NOT PUBLISHED", "EXPIRED ALREADY", "CURRENTLY PUBLISHED"}; 
            ViewBag.Published = new SelectList(publishedStatuses, "NOT PUBLISHED");

            var articles =
            (
                    from p in db.Articles.ToList()
                    where p.PublishToWeb == false && p.Menu == "Home"                  
                    select p
             );

            }
            return View(articles);
        }              
} 

回答1:


Your problem here is that the link you're clicking to page will call the Admin controller action again, and since you're not receiving any parameters it will do the initial search again.

If you change the action method as:

public ActionResult Admin(int? page)
{
   //Your code here
}

You will get the page number from the web grid and can use that in your search. To enable filtering and searchgin, have a look at this blog post:

http://www.elylucas.net/post/Using-a-grid-that-can-sort-page-and-filter-in-AspNet-MVC3e28093Part-1e28093Using-the-WebGrid-WebHelper.aspx



来源:https://stackoverflow.com/questions/8999390/mvc-3-webgrid-paging-doesnt-return-orginal-search-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!