(7)为结果排序
来源:互联网 发布:wind量化接口python 编辑:程序博客网 时间:2024/06/09 17:09
你有一个很庞大的列表(例如,图书列表),你不能很容易找到你想找的东西。以列表中某一列为基础排序,可以帮助你更快的去找到你想要的东西。
解决方案
在book list的标题上添加一个链接。当用户点击链接的时候,使用Dynamic Linq Library去为结果排序,给予选择的列。(升序或者降序)。再点一次链接的话,就会反转顺序。
讨论
和以前我用过的框架相比较,我对于在自动生成的View里添加一个排序有点惊讶。希望在未来版本的MVC中,脚手架可以帮我们去做这件事。另一件我需要做的事就是在ASP.NET MVC 的主页上提供一个或更多的选项可以去切换排序。在图书的例子里,只有5个列需要被排序,也不算太糟。但是如果这个功能应用到其他的例子上。可能是作者列等,工作量就要增加啦!在下边的例子,我们如果用Dynamic Linq Library就简单多啦!
默认的,linq 类库 允许强类型的表达式从数据库生成结果。这有一些优势,比如,全方位的智能感知,编译时错误消息。当然,就像我上边说的,Build那些有用的功能也变成了很大的工作量。
为了支持排序,在BooksController 和Books index view 需要做以下更改:
@model IEnumerable<
MvcApplication.Models.Book
>
<
h2
>@ViewBag.Title</
h2
>
<
p
>
@Html.ActionLink((string)ViewBag.CreateLink, "Create")
</
p
>
<
table
>
<
tr
>
<
th
>
@Html.ActionLink((string)ViewBag.TitleDisplay,
"Index", new { sortOrder = ViewBag.TitleSortParam })
</
th
>
<
th
>
@Html.ActionLink((string)ViewBag.IsbnDisplay,
"Index", new { sortOrder = ViewBag.IsbnSortParam })
</
th
>
<
th
>
@ViewBag.SummaryDisplay
</
th
>
<
th
>
@Html.ActionLink((string)ViewBag.AuthorDisplay,
"Index", new { sortOrder = ViewBag.AuthorSortParam })
</
th
>
<
th
>
@ViewBag.ThumbnailDisplay
</
th
>
<
th
>
@Html.ActionLink((string)ViewBag.PriceDisplay,
"Index", new { sortOrder = ViewBag.PriceSortParam })
</
th
>
<
th
>
@Html.ActionLink((string)ViewBag.PublishedDisplay,
"Index", new
{
sortOrder =
ViewBag.PublishedSortParam
})
</
th
>
<
th
>
</
th
>
</
tr
>
@foreach (var item in Model)
{
<
tr
>
<
td
>
@Html.DisplayFor(modelItem => item.Title)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Isbn)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Summary)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Author)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Thumbnail)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Price)
</
td
>
<
td
>
@Html.DisplayFor(modelItem => item.Published)
</
td
>
<
td
>
@Html.ActionLink((string)ViewBag.EditLink,
"Edit", new { id = item.ID }) |
@Html.ActionLink((string)ViewBag.DetailsLink,
"Details", new { id = item.ID }) |
@Html.ActionLink((string)ViewBag.DeleteLink,
"Delete", new { id = item.ID })
</
td
>
</
tr
>
}
</
table
>
在上边的例子中,前边创建的<th>标签已不再是静态的文本。已经使用ActionLink替换成了HTML Helper中的HTML link。
接下来BookController的 Index action也需要被更新。这个action将要接收一个新的参数: sortOrder。接下来我们要使用LINQ 根据这列为结果排序。微软已经提供了一个免费的 动态查询类,它是linq下的扩展,允许你根据表达式动态的查询。命名空间为using System.Linq.Dynamic。下载这个类的C#版本可以访问:http://msdn2.microsoft.com/en-us/vcsharp/bb894665.aspx.(译者:记得点下边的 I accept啊!)下载完,你需要解压缩。动态linq查询类可以在这里被找到: ~CSharpSamplesLinqSamplesDynamic
QueryDynamicQueryDynamic.cs. 这个文件一定要加到项目里。为了合理组织项目结构。我建议你把他加到Utils目录下。右击Utils目录->添加->已经存在的项,并找到dynamic linq
class (或者你直接把那个文件夹拖拽到Utils目录下)。
添加完成后,按照如下代码更新BooksController:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using MvcApplication.Models;
using MvcApplication.Resources;
namespace MvcApplication.Controllers
{
public class BooksController : Controller
{
private BookDBContext db = new BookDBContext();
//
// GET: /Books/
public ViewResult Index(string sortOrder)
{
#region ViewBag Resources
#endregion
#region ViewBag Sort Params
// Define the sort orders - if the same link is
// clicked twice, reverse the direction from
// ascending to descending
ViewBag.TitleSortParam = (sortOrder == "Title")
? "Title desc" : "Title";
ViewBag.IsbnSortParam = (sortOrder == "Isbn")
? "Isbn desc" : "Isbn";
ViewBag.AuthorSortParam = (sortOrder == "Author")
? "Author desc" : "Author";
ViewBag.PriceSortParam = (sortOrder == "Price")
? "Price desc" : "Price";
ViewBag.PublishedSortParam =
(String.IsNullOrEmpty(sortOrder))
? "Published desc" : "";
// Default the sort order
if (String.IsNullOrEmpty(sortOrder))
{
sortOrder = "Published desc";
}
#endregion
var books = db.Books.OrderBy(sortOrder);
return View(books.ToList());
}
}
}
上边的例子允许根据传入的sortOrder 变量排序。上面的代码是轻微的不安全,是为了证明 以最小的影响执行动态LINQ查询的过程。由于这个变量可以通过URL传入,为input数据添加一些验证是十分重要的,这样可以确保用户没法进行恶意行为。
- (7)为结果排序
- 大根堆排序结果为升序
- (8)为列表结果分页
- 下列代码之后的结果为()?
- (93&-8)的结果为
- SQL技巧(2) 查询结果排序
- 排序(如12,3,45,2,74。结果为:12,2,3,45,74)
- MyBatis动态参数为数组、list、Map,并使用oder by给结果排序
- 解决mysql排序字段的数据为中文时不能正常显示结果的问题
- 得到随机排序结果
- 搜索结果排序
- 结果集排序
- 得到随机排序结果
- 结果集排序
- Hibernate查询结果排序
- List结果集排序
- (003):排序结果集
- Hibernate查询结果排序
- (5)发送欢迎邮件
- (3)验证用户的输入
- DirectShow 使用时个人整理的一些常见问题和解决方案
- (6)找回忘记的密码
- 验证码的读秒数-js
- (7)为结果排序
- (8)为列表结果分页
- (9)在结果中筛选
- (10)根据关键字搜索
- 使用mongodb shard cluster构建高可用的系统架构
- 35岁前必须做好的10件事情(转载)
- xCode 4.X 免证书真机发布及调试
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理 - by Glede
- (11)通过表单上传文件