WebGrid with filtering, paging and sorting

by Jose M. Aguilar on April 24, 2012 in Web Development

A few days ago I received some questions on the use of the Webgrid helper in the comments section of my personal blog, specifically on how to implement custom filters on the data displayed. Since it’s not easy to answer through the comments section, I have forced myself to write an article on it. Here it is.

The main obstacle here is that WebGrid doesn’t include any kind of tool to perform this frequent task. It simply displays the data we provide, and if we want to filter we have to do it manually. In theory, it would be enough to just to supply the Controller the filters we want to apply. Then the Controller queries the info from the Model following the criteria set.

However, something that may seem relatively simple gets harder if we want to also keep the sort order and paging capabilities untouched because we have to arrange for state maintenance between requests as well. Well, this is something we can solve in a few minutes ;-)

Starting out from the project developed in this post of the series on Webgrid, let’s see how we can add a couple of search criteria to the grid, so it can appear to be this way:

The text field enables us to look for substrings in the names and surnames of the people stored in the database, and the other two fields allow to filter by range (min-max) the children they have. As usual, we’ll combine these conditions with a logic AND.

1. The View

First, we enter the form we are going to use to request the user the search criteria in the view, right before generating the grid:

The code is as follows:

@using(Html.BeginForm(null, null, FormMethod.Get))
{
<fieldset>
<legend>Search criteria</legend>
@Html.Label("search", "Text:")
@Html.TextBox("search")
@Html.Label("minChildren", "Children min:")
@Html.TextBox("minChildren", null, new { @class="num" })
@Html.Label("maxChildren", "Children max:")
@Html.TextBox("maxChildren", null, new { @class="num"} )
<input type="submit" value="Apply" />
</fieldset>
}

Notice the simplicity of the form. We don’t even have to use the lambda syntax in the edit helpers. We generate the controllers starting out from the fields whose values will be in the query string (and not from the Model, which is the usual thing to do). For this reason, notice that the form is set to be sent using the HTTP GET method.

This way we can propagate easily the values of the controls (textbox) between calls:

  • if the user enters criteria and clicks the send button, the URL the request is performed on will be, for instance, /friends/index?search=aguilar&minChildren=1&maxChildren=4.

  • if the user uses the grid’s navigation buttons (next/previous page, go to page, or reorder), these parameters will be added to the previous ones, and therefore they preserve their values between the different calls. This is because Webgrid generates the links to these actions preserving the current query string parameters. In other words, if we are filtering and we go to page 4, we access an address that includes both the search criteria info as well as the paging, something like: /friends/index?search=aguilar&minChildren=1&maxChildren=4&page=4.

And by doing so, we the view layer is finished.

2. The controller

The action method in charge of getting the grid data and sending the view with the data to the user receives three parameters: current page, sort order field and its direction (ascending/descending).

Since now we have to get the sort order criteria, we have to extend its definition adding parameters for these values:

public ActionResult Index(int page = 1, string sort = "surname",
string sortDir = "ASC", string search = null,
int? minChildren = null, int? maxChildren = null)

See how all the parameters are optional, and we set them to null to easily detect when they come filled.

And at which point do we need to use these new parameters? Only in two:

  • in the call we make to the Model to count the total amount of grid rows. We have to inform the grid on the filtering criteria so the counting is performed properly.

  • in the call we make to the Model to get the rows to be displayed in the current page, where we obviously have to take into account the filters.

The action is results somewhat like this:

public ActionResult Index(int page = 1, string sort = "surname",
string sortDir = "ASC", string search = null,
int? minChildren = null,
int? maxChildren = null)
{
var friendsCount = _services.GetFriendsCount(search, minChildren, maxChildren);
var friends = _services.GetFriendsPage(page, FRIENDS_PER_PAGE, sort, sortDir,
search, minChildren, maxChildren); var data = new FriendsPageViewModel()
{
NumberOfFriends = friendsCount,
FriendsPerPage = FRIENDS_PER_PAGE,
Friends = friends
}; return View(data);
}

And this is all in the comtroller.

3. The Model

And at last, now in the Model, we have to make the methods used from the controller (GetFriendsCount and GetFriendsPage) take into account the parameters in which we indicate the search conditions.

In the first one, we simply return the number of people which follow the criteria returned as parameters:

public int GetFriendsCount(string searchText = null, int? minChildren = null,
int? maxChildren = null)
{
IQueryable<Friend> query = _data.People;
query = filterPeople(searchText, minChildren, maxChildren, query);
return query.Count();
}

The helper method filterPeople() we use is only in charge of adding the where clauses to the query that we need to take into account the specified conditions:

private static IQueryable<Friend> filterPeople(
string searchText, int? minChildren,
int? maxChildren, IQueryable<Friend> query)
{
if (!string.IsNullOrWhiteSpace(searchText))
query = query.Where(p => p.Name.Contains(searchText)
|| p.Surname.Contains(searchText));
if (maxChildren != null)
query = query.Where(p => p.Children <= maxChildren);
if (minChildren != null)
query = query.Where(p => p.Children >= minChildren);
return query;
}

At last, we implement the method that obtains the data to display on the current page:

public IEnumerable<Friend> GetFriendsPage(int currentPage, int friendsPerPage,
string sortColumn, string sortDir,
string searchText, int? minChildren,
int? maxChildren)
{
// Validate input data
sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ?
sortDir : "asc"; var validColumns = new[] { "surname", "birthdate", "email", "children" };
if (!validColumns.Contains(sortColumn.ToLower()))
sortColumn = "surname"; if (currentPage < 1) currentPage = 1;
if (friendsPerPage < 1) friendsPerPage = 10; // Create the query
var query = (IQueryable<Friend>)_data.People
.OrderBy("it." + sortColumn + " " + sortDir); query = filterPeople(searchText, minChildren, maxChildren, query); return query
.Skip((currentPage - 1) * friendsPerPage)
.Take(friendsPerPage)
.ToList();
}

There is not much to say about this code. In first place, a basic check of the entry parameters is performed, and then generate the query to be performed on the database. As you can see, the Como podéis observar, the queryPeopleFiltered() method is performed to apply the query criteria.

Summarizing…

As we have seen, implementing search criteria in Webgrid doesn’t differ much from what we have described earlier on in my other posts about WebGrid. We just have take into account the following points:

  • firstly, include a form in the View where the query criteria is collected in order to send it to the controller.

  • secondly, prepare the Controller so it can receive this criteria and make it reach the Model.
  • thirdly, in the Model, simply apply this criteria when countimg the total amount of rows, and when obtaining the data to be displayed in the grid page.

Here you can download the example code for this post.

WebGrid with filtering, paging and sorting 【转】的更多相关文章

  1. Spring REST实践之Versioning,Paging和Sorting

    Versioning 为适应需求的变化以及兼容已有的API,需要创建新版本的API,一般有四种流行的版本化API的方法: URI版本化 URI参数版本化 Accept header版本化 自定义hea ...

  2. [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...

  3. [转]Paging, Searching and Sorting in ASP.Net MVC 5

    本文转自:http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc- ...

  4. WebGrid Helper with Check All Checkboxes

    WebGrid Helper with Check All Checkboxes myEvernote Link Tuesday, September 13, 2011ASP.NET ASP.NET ...

  5. [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)

    Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...

  6. [转]Efficiently Paging Through Large Amounts of Data

    本文转自:http://msdn.microsoft.com/en-us/library/bb445504.aspx Scott Mitchell April 2007 Summary: This i ...

  7. ExtJS笔记 Proxy

    Proxies are used by Stores to handle the loading and saving of Model data. Usually developers will n ...

  8. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  9. jQuery DataTables and ASP.NET MVC Integration

    part 1 : http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Pa ...

随机推荐

  1. 数据库开发基础 SQL Server 数据库的备份、还原与分离、附加

    认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是 一.在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点 二. ...

  2. WordPress菜单函数wp_nav_menu()详细介绍

    导航菜单函数wp_nav_menu()进行详细的说明. 1.wp_nav_menu()函数介绍: worpdress发展到3.0以后增加了一个自定义菜单函数wp_nav_menu(),使得wordpr ...

  3. Android studio:Groovy 与 Gradle 基础【三】

    转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=255064&extra=page%3D2%26filter%3Dautho ...

  4. [bzoj1618][Usaco2008 Nov]购买干草

    Description    约翰的干草库存已经告罄,他打算为奶牛们采购磅干草.    他知道个干草公司,现在用1到给它们编号.第个公司卖的干草包重量为磅,需要的开销为美元.每个干草公司的货源都十分充 ...

  5. js-JavaScript高级程序设计学习笔记9

    依然第十三章 事件 1.页面上的所有元素都支持鼠标事件,除了mouseenter和mouseleave,所有鼠标事件都会冒泡. 2.修改键:shift.ctrl.alt.meta.四个属性表示修改键的 ...

  6. C#点击按钮关闭当前窗体 打开另一个窗体。

    网上有很多是隐藏当前窗体,但是这样占用资源,效果不好,因此改进方法如下: private void button1_Click(object sender,EventArgs e) { this.hi ...

  7. 第六次作业——利用MFC实现计算器图形界面以及简单四则运算表达式批处理

    参考资料:      1.MFC响应键盘      2.计算器实例      3.MFC文件对话框      4.MFCUpdateData()函数的使用      5.MFC教程      6.wi ...

  8. JSP重定向小例子(不讲原理)

    编写一个JSP页面lucknum.jsp,产生0-9之间的随机数作为用户幸运数字,将其保存到会话中,并重定向到另一个页面showLuckNum.jsp中,在该页面中将用户的幸运数字显示出来 luckn ...

  9. SQLite的WAL机制

    标注:本文部分有黏贴这里的资料,另外还加了一些自己的笔记 使用CoreData或者SQLite3的时候,我们创建的数据库, 在存储的文件夹中有三个文件:分别为:**.sqlite  **.sqlite ...

  10. NOIp 0916 爆零记

    题目来自神犇chad 上次爆零是说着玩,这次真的爆零了QAQ 好吧貌似是TYVJ的模拟赛打多了..一直按照TYVJ的格式提交的压缩包.. 然后莫名其妙就AK了hhh 来的时候迟到了半小时,昨晚痛苦的补 ...