问题
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