I have 2 Search actions one for get and other for
Post
public ActionResult Search()
{
/*code to populate dropdowns, listboxes and other controls that form search criteria
*/
return View();
}
[HttpPost]
public ActionResult Search(FormCollection collection)
{
/*get search criteria from Formcollection and get data*/
List<SearchSummary> searchRecords = summaryList.Summaries;
ViewBag.Records = searchRecords;
return View(ViewBag);
}
Now i am using a WebGrid to show all records. I am not getting any data when i am clicking on
Grid SortColumn and Page number links.
This is happening because it is going for first Search() whereas all data fetch login is in another method
Search(FormCollection collection).
Please help me understand how to fix this. Also plz provide any gud site with gud example on webgrid.
What does the code in the view look like? Specifically where the webgrid is declared.
It looks like you are fetching the data in the HTTP post search action, I believe by default the web grid pages data by performing an http get (the first option). If you do not want to use ajax, then you have to repopulate the grid data.
Another thing to be careful of is that you are getting your data as a list instead of an IQueryable. In this case even if the grid has paging, you are pulling ALL the data from the database, which would cause a performance hit over large sets of data.
I don't think you need the Search action. You just need pass the data in the Index action, and specify two paramters, one is sortOrder and anothers one is page.
SanjaySutar
Participant
1128 Points
692 Posts
MVC 3 WebGrid paging and sorting issue
Mar 27, 2012 12:09 PM|LINK
Hi,
I have 2 Search actions one for get and other for Post
public ActionResult Search() { /*code to populate dropdowns, listboxes and other controls that form search criteria */ return View(); } [HttpPost] public ActionResult Search(FormCollection collection) { /*get search criteria from Formcollection and get data*/ List<SearchSummary> searchRecords = summaryList.Summaries; ViewBag.Records = searchRecords; return View(ViewBag); }Now i am using a WebGrid to show all records. I am not getting any data when i am clicking on Grid SortColumn and Page number links.
This is happening because it is going for first Search() whereas all data fetch login is in another method Search(FormCollection collection).
Please help me understand how to fix this. Also plz provide any gud site with gud example on webgrid.
Thanks in advance.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MVC 3 WebGrid paging and sorting issue
Mar 27, 2012 03:33 PM|LINK
What does the code in the view look like? Specifically where the webgrid is declared.
It looks like you are fetching the data in the HTTP post search action, I believe by default the web grid pages data by performing an http get (the first option). If you do not want to use ajax, then you have to repopulate the grid data.
Another thing to be careful of is that you are getting your data as a list instead of an IQueryable. In this case even if the grid has paging, you are pulling ALL the data from the database, which would cause a performance hit over large sets of data.
Blog | Twitter : @Hattan
SanjaySutar
Participant
1128 Points
692 Posts
Re: MVC 3 WebGrid paging and sorting issue
Mar 27, 2012 03:45 PM|LINK
thanks for the reply....
I am populating on HTTP Post because I have to populate grid on click of a "Search" button.
The grid gets populated, but when i click on any sort column or page number, it goes back to HTTP Get action.
Also I would change the code to return IQueryable object.
At the moment i am concernd about sort columns and page number links
What is the right way to implement the grid ??? Any example of that ??
Young Yang -...
All-Star
21111 Points
1817 Posts
Microsoft
Re: MVC 3 WebGrid paging and sorting issue
Apr 03, 2012 07:32 AM|LINK
Hi
I don't think you need the Search action. You just need pass the data in the Index action, and specify two paramters, one is sortOrder and anothers one is page.
For details, you can have a looke:
http://msdn.microsoft.com/en-us/magazine/hh288075.aspx
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store