ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】
ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting
FEBRUARY 27, 2012 14 COMMENTS
WebGrid is a very powerful HTML helper component introduced with ASP.NET MVC 3 and ASP.NET Web Pages. Despite all its cool features, it is troublesome to use it in real enterprise web applications since it does not have true AJAX support for sorting and pagination.
Its AJAX support is limited to retrieving the full page and then filtering out the contents of the container div tag using jQuery. This is even worse than UpdatePannel in ASP.NET WebForms (UpdatePannel only return the HTML that needs to be updated rather than the full HTML page). If the page has lots of other data (outside the grid) to display or if it has multiple grids, then the situation become worse. This support is sufficient for ASP.NET Web Page applications. However this is not what is expected from an ASP.NET MVC application.
However we can easily find a solution to this problem using jQuery. This can be achieved as follows.
The first step is to separate the grid implementation into a partial page that is loaded via a separate controller action. In my example, I assume that the grid is displayed using index action of the StudentController class. Then I break this index action into two actions (by introducing new “StudentGrid” action) as shown below.
public const int PageSize = 4;
public const string StudentGridAction = "StudentGrid"; public ActionResult Index()
{
return View();
} [ActionName(StudentGridAction)]
public ActionResult StudentGrid(string page, string sort, string sortdir)
{
int pageNo = 0;
if (!string.IsNullOrEmpty(page))
{
pageNo = int.Parse(page) - 1;
} var result = GetStudents(sort, sortdir, pageNo);
var model = new StudentGridModel();
model.Students = result.Students;
model.RowCount = result.Count;
model.CurrentPage = pageNo;
return PartialView(model);
}
Then I break the corresponding index.cshtml file into cshtml files as shown below,
Index.cshtml file:
@using MyApplicationNs.Controllers
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div id="studentGrid">
@Html.Action(StudentController.StudentGridAction)
</div>
StudentGrid.cshtml file:
@model MyApplicationNs.Models.Student.StudentGridModel
@using MyApplicationNs.Controllers
@{
WebGrid studentGrid = new WebGrid(rowsPerPage: StudentController.PageSize);
studentGrid.Bind(Model.Students, autoSortAndPage: false, rowCount: Model.RowCount);
studentGrid.PageIndex = Model.CurrentPage;
} @studentGrid.GetHtml(columns: new WebGridColumn[]{
studentGrid.Column("StudentId", "Id"),
studentGrid.Column("Name", "Name"),
studentGrid.Column("Address","Address")})
After this refactoring, my grid works exactly as it worked before.
In order to ensure that only the grid content generated using new “StudentGrid” action is loaded when pagination and sorting links are clicked, I have added the following JavaScript block that uses jQuery at the bottom of index.csthml page.
This JavaScript register an event handler to the click event of every hyperlink in the container div tag of the grid using jQuery ‘live’ method (‘live’ method ensures that this event handler is applied to future hyperlinks that will be loaded using AJAX operations we well). This click event extracts the query string portion of the URL in each link, append it into action URL for “StudentGrid” action and then load the contents of the container div by sending AJAX request to that URL (using jQuery “load” method).
<script type="text/javascript"> $(document).ready(function () {
$("#studentGrid a").live('click', function (event) {
event.preventDefault();
var href = $(this).attr("href");
var queryString = href.substring(href.indexOf('?'), href.length);
var requestUrl =
'@Url.Action(StudentController.StudentGridAction)' + queryString;
$("#studentGrid").load(requestUrl);
});
}); </script>
Thanks to jQuery authors, this simple script has perfectly solved the problem. Now, when a pagination or sorting link is clicked, the request is sent to StudentGrid action, that will only return the contents of the grid.
ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】的更多相关文章
- 14、ASP.NET MVC入门到精通——Ajax
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 Unobtrusive Ajax使用方式(非入侵式) 非入侵式,通俗来讲:就是将嵌入在Html中的JavaScript全部取出来,放在单独的 ...
- 转:ASP.Net MVC:校验、AJAX与过滤器
原文地址:http://blog.jobbole.com/85005/ 一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.Componen ...
- ASP.NET MVC中如何以ajax的方式在View和Action中传递数据
前言:写这篇随笔的时候,在url上漏写了斜线,找了好久错误,整个人都很不好.#我是猪系列 背景:之前介绍过一篇如何构建 MVC&AJax&JSon示例,这一篇单独讲解如何在View和A ...
- Asp.net MVC 视图使用像Ajax,ViewBag提示为找到上下文
不知是什么原因,所有的视图中Ajax,ViewBag之类的都提示为找到上下文(由于换了个版本Vs,猜测应该是Vs的原因),然后顺利在网上找到了解决方案. 给地址链接:https://social.ms ...
- Asp.net mvc中的Ajax处理
在Asp.net MVC中的使用Ajax, 可以使用通用的Jquery提供的ajax方法,也可以使用MVC中的AjaxHelper. 这篇文章不对具体如何使用做详细说明,只对于在使用Ajax中的一些需 ...
- Asp.Net MVC Ajax
将ASP.NET MVC中的form提交改为ajax提交 在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascrip ...
- ASP.NET MVC Ajax 伪造请求
1.前言 CSRF(Cross-site request forgery)跨站请求伪造,ASP.NET MVC 应用通过使用AJAX请求来提升用户体验,浏览器开发者工具可以一览众山小,就很容易伪造了请 ...
- ASP.NET MVC 入门11、使用AJAX
asp.net mvc 支持微软自身Ajax 和 JQuery框架 asp.net mvc View视图可以理解为 一个包含"<%%>"变量引和的模板. Script与 ...
- APS.NET MVC + EF (10)---使用AJAX
在Web系统中,Ajax技术已经成为提高用户体验的必备技术.开发Ajax程序,涉及两方面的内容:一是客户端技术,二是服务器端技术. (1)客户端技术 核心工作是通过JavaScript向服务器发送数据 ...
随机推荐
- 数据库开发基础 SQL Server 数据库的备份、还原与分离、附加
认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是 一.在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点 二. ...
- JQuery 判断浏览器及其版本
JQuery 中用 方法 jQuery.browser 来判断浏览器,返回值可以为: safari opera msie mozilla. 当然有时候我们还需要区分版本 这就要用到 jQuery.br ...
- 【BZOJ-1568】Blue Mary开公司 李超线段树 (标记永久化)
1568: [JSOI2008]Blue Mary开公司 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 557 Solved: 192[Submit ...
- Hadoop的RPC分析
一.基础知识 原理 http://www.cnblogs.com/edisonchou/p/4285817.html,这个谢了一些rpc与hadoop的例子. 用到了java的动态代理,服务端实现一个 ...
- ajax版本带搜索的下拉框
demo地址:https://github.com/aa1356889/MyAjaxSelectSearch
- Jenkins项目构建结果通知Email的替代方案Wall Display插件
插件:https://wiki.jenkins-ci.org/display/JENKINS/Wall+Display+Plugin 效果: 操作: 直接在一个大屏幕上打开这个项目的构建页面,那么每次 ...
- 洛谷P1726 上白泽慧音
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- Linux 开机 logo 修改
从内核被解压到文件系统被挂载,我们看到的经典画面是一个小企鹅.如果嫌小企鹅枯燥,我们可以把它换掉. 1. 准备图片 这里需要的是 ppm 图片,所以,我们需要把常见格式给转换为 .ppm 才能使用.c ...
- win7、win8上SaveFileDialog窗口跳不出的问题
xp上做的开一个线程 线程中数据以Excel形式保存到指定文件中的程序 放到win7 win8上都不跳出保存的对话框? 解决: 在win7.win8上都要对线程 在线程启动前设置其单元状态.设置为 ...
- Oracle 应用于.NET平台
1. 回顾ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库集,它存在于.Net Framework中.通常情况下,数据源可以是各种类型的数据库,利用ADO.NET可以访问目前几乎 ...