[翻译][MVC 5 + EF 6] 3:排序、过滤、分页
原文:Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
1.添加排序:
1.1.修改Controllers\StudentController.cs的Index:
- public ActionResult Index(string sortOrder)
- {
- ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
- ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
- var students = from s in db.Students
- select s;
- switch (sortOrder)
- {
- case "name_desc":
- students = students.OrderByDescending(s => s.LastName);
- break;
- case "Date":
- students = students.OrderBy(s => s.EnrollmentDate);
- break;
- case "date_desc":
- students = students.OrderByDescending(s => s.EnrollmentDate);
- break;
- default:
- students = students.OrderBy(s => s.LastName);
- break;
- }
- return View(students.ToList());
- }
1.2.修改Views\Student\Index.cshtml:
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table>
- <tr>
- <th>
- @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
- </th>
- <th>First Name
- </th>
- <th>
- @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
- </th>
- <th></th>
- </tr>
效果图:
2.添加搜索框:
2.1.修改Controllers\StudentController.cs的Index:
- public ViewResult Index(string sortOrder, string searchString)
- {
- ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
- ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
- var students = from s in db.Students
- select s;
- if (!String.IsNullOrEmpty(searchString))
- {
- students = students.Where(s => s.LastName.Contains(searchString)
- || s.FirstMidName.Contains(searchString));
- }
- switch (sortOrder)
- {
- case "name_desc":
- students = students.OrderByDescending(s => s.LastName);
- break;
- case "Date":
- students = students.OrderBy(s => s.EnrollmentDate);
- break;
- case "date_desc":
- students = students.OrderByDescending(s => s.EnrollmentDate);
- break;
- default:
- students = students.OrderBy(s => s.LastName);
- break;
- }
- return View(students.ToList());
- }
在大多数情况下,我们调用同一个方法时,在EF实体集和内存中集合的扩展方法返回的结果是一样的;但是在一些情况下返回结果会不一样。
例如当传递一个空的字符串时.NET Framework的Contains方法返回所有行,但是SQL Server Compact 4.0的EF 提供者(provider)的Contains返回0行。因此上面的代码中把Where语句放在if语句中,以保证对所有的SQL Server版本返回相同的数据。同样,.NET Framework的Contains方法默认是区分大小写的,因此,使用ToUpper方法将字符串显示转换为大写以确保以后代码改为仓储时(返回类型由IQueryable
类型变成IEnumerable
类型)返回结果不会发生变化(当调用Contains方法返回IEnumerable
集合时,调用的是.NET Framework实现;返回IQueryable
对象时,调用的是数据库提供者的实现)。
当我们返回IQueryable
对象时,不同的数据库提供者对空值的处理也不同。例如,在有些脚本中where条件中的table.Column != 0
可能不会返回空值列。
2.2.为Views\Student\Index.cshtml添加搜索框:
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- @using (Html.BeginForm())
- {
- <p>
- Find by name: @Html.TextBox("SearchString")
- <input type="submit" value="Search" />
- </p>
- }
效果图:
3.添加分页:
3.1.安装PagedList.MVC:
- Install-Package PagedList.Mvc
3.2.修改Controllers\StudentController.cs:
- using PagedList;
- public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
- {
- ViewBag.CurrentSort = sortOrder;
- ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
- ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
- if (searchString != null)
- {
- page = ;
- }
- else
- {
- searchString = currentFilter;
- }
- ViewBag.CurrentFilter = searchString;
- var students = from s in db.Students
- select s;
- if (!String.IsNullOrEmpty(searchString))
- {
- students = students.Where(s => s.LastName.Contains(searchString)
- || s.FirstMidName.Contains(searchString));
- }
- switch (sortOrder)
- {
- case "name_desc":
- students = students.OrderByDescending(s => s.LastName);
- break;
- case "Date":
- students = students.OrderBy(s => s.EnrollmentDate);
- break;
- case "date_desc":
- students = students.OrderByDescending(s => s.EnrollmentDate);
- break;
- default: // Name ascending
- students = students.OrderBy(s => s.LastName);
- break;
- }
- int pageSize = ;
- int pageNumber = (page ?? );
- return View(students.ToPagedList(pageNumber, pageSize));
- }
当页面第一次加载或者用户没有点击分页或排序链接时,所有的参数都是空值。
ViewBag.CurrentSort用于点击分页时保存排序信息。
ViewBag.CurrentFilter
用于保存搜索信息,并且当页面刷新时给搜索框赋值。当搜索条件改变时,page值重置为1。
3.3.修改Views\Student\Index.cshtml:
- @model PagedList.IPagedList<ContosoUniversity.Models.Student>
- @using PagedList.Mvc;
- <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
- @{
- ViewBag.Title = "Students";
- }
- <h2>Students</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- @using (Html.BeginForm("Index", "Student", FormMethod.Get))
- {
- <p>
- Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
- <input type="submit" value="Search" />
- </p>
- }
- <table class="table">
- <tr>
- <th>
- @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
- </th>
- <th>
- First Name
- </th>
- <th>
- @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter=ViewBag.CurrentFilter })
- </th>
- <th></th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.LastName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.FirstMidName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.EnrollmentDate)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
- @Html.ActionLink("Details", "Details", new { id=item.ID }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.ID })
- </td>
- </tr>
- }
- </table>
- <br />
- Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
- @Html.PagedListPager(Model, page => Url.Action("Index",
- new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
BeginForm提交form数据的默认方式是post,post方式是通过HTTP消息体而不是通过URL查询字符串传递参数。我们通过FormMethod.Get把它指定为HTTP GET,这样用户就可以将URL收藏为书签。当动作不会导致更新时W3C HTTP GET使用指南推荐我们使用GET。
效果图:
4.添加About页面展示Student统计信息:
4.1.创建视图模型:
新建ViewModels文件夹EnrollmentDateGroup.cs:
- public class EnrollmentDateGroup
- {
- [DataType(DataType.Date)]
- public DateTime? EnrollmentDate { get; set; }
- public int StudentCount { get; set; }
- }
4.2.修改HomeController.cs:
- private SchoolContext db = new SchoolContext();
- //...
- public ActionResult About()
- {
- IQueryable<EnrollmentDateGroup> data = from student in db.Students
- group student by student.EnrollmentDate into dateGroup
- select new EnrollmentDateGroup()
- {
- EnrollmentDate = dateGroup.Key,
- StudentCount = dateGroup.Count()
- };
- return View(data.ToList());
- }
- //...
- protected override void Dispose(bool disposing)
- {
- db.Dispose();
- base.Dispose(disposing);
- }
4.3.修改Views\Home\About.cshtml:
- @model IEnumerable<ContosoUniversity.ViewModels.EnrollmentDateGroup>
- @{
- ViewBag.Title = "Student Body Statistics";
- }
- <h2>Student Body Statistics</h2>
- <table>
- <tr>
- <th>
- Enrollment Date
- </th>
- <th>
- Students
- </th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.EnrollmentDate)
- </td>
- <td>
- @item.StudentCount
- </td>
- </tr>
- }
- </table>
效果图:
[翻译][MVC 5 + EF 6] 3:排序、过滤、分页的更多相关文章
- asp.net core 排序过滤分页组件:sieve(1)
使用asp.net core开发时避免不了要用一个合适的分页组件来让前端获取分页数据.github上面有一个开源的分页组件在这方面很适合我的使用,于是我把他的文档翻译一下,随后会分析它里面的源码.这是 ...
- [翻译][MVC 5 + EF 6] 12[完结]:高级场景
原文:Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application 1.执行原生SQL查询: EF Code First API ...
- [翻译][MVC 5 + EF 6] 11:实现继承
原文:Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.选择继承映射到数据库 ...
- [翻译][MVC 5 + EF 6] 10:处理并发
原文:Handling Concurrency with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.并发冲突: 当一个用户编辑一 ...
- [翻译][MVC 5 + EF 6] 9:异步和存储过程
原文:Async and Stored Procedures with the Entity Framework in an ASP.NET MVC Application 1.为什么使用异步代码: ...
- [翻译][MVC 5 + EF 6] 8:更新相关数据
原文:Updating Related Data with the Entity Framework in an ASP.NET MVC Application 1.定制Course的Create和E ...
- [翻译][MVC 5 + EF 6] 7:加载相关数据
原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 1.延迟(Lazy)加载.预先(Eage ...
- [翻译][MVC 5 + EF 6] 6:创建更复杂的数据模型
原文:Creating a More Complex Data Model for an ASP.NET MVC Application 前面的教程中,我们使用的是由三个实体组成的简单的数据模型.在本 ...
- [翻译][MVC 5 + EF 6] 5:Code First数据库迁移与程序部署
原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 1.启用 ...
随机推荐
- 安装PHP出现make: *** [sapi/cli/php] Error 1 解决办法
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':/home/king/php-5.2.13/ext/iconv/ ...
- 注册表-各种功能-隐藏IE、隐藏硬盘、禁用硬件
1.在[我的电脑]上隐藏软驱 在[开始]→[运行]→输入[Regedit]→[HKEY_CURRENT_USER]→[Software] →[Microsoft] →[Windows]→[Curren ...
- 说一说window.parent
<iframe>标签是很常用的,嵌在页面之中,可以做独立的加载和刷新.比如说,页面分左右或者上下结构,一般左侧和上侧是导航部分,右侧和下侧是目标页面的展示部分,只需要设置导航链接的targ ...
- iOS 使用fir、 蒲公英 进行内部测试
fir 蒲公英需要去注册账号并认证,按提示即可完成. 测了公司账号.个人开发账号,2个都可以用,就是要在配置文件里加上测试者的udid. 步骤: 1.添加测试机的udid edit配置文件,添加刚刚加 ...
- JBPM学习(二):ProcessEngine与Service API
1.获取processEngine的方法: a) 方法一 private ProcessEngine processEngine = new Configuration().setResource(& ...
- lettCode-Array
1 Remove Element lintcode-172 描述: 删相同元素,反现有长度 记忆:标不同元素,反标记值 public int removeElement(int[] a, i ...
- mysqldump 备份原理8
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; http://www.cnblogs.com/lyhabc/p/38 ...
- Qt之QtScript(一)
C++和JavaScript相互通信.今天就学习QtScript模块吧. Qt 包含完全集成的 ECMA 标准脚本引擎.Qt Script 集成了 QObject,为脚本提供了 Qt 的信号与槽 (S ...
- Android自定义View之ProgressBar出场记
关于自定义View,我们前面已经有三篇文章在介绍了,如果筒子们还没阅读,建议先看一下,分别是android自定义View之钟表诞生记.android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检 ...
- Linux下如何在打开终端的时候自动配置相关环境
参考博客“Linux启动文件.设置环境变量的位置”(http://www.2cto.com/os/201305/208251.html) 在不可取的root权限的时候可以选择编辑~/.bashrc文件 ...