灵活掌握Asp.net MVC中GridView的使用方法(3)

/// <summary> /// 启用查询谓词的高效,动态组合。 /// </summary> public static class PredicateBuilder { /// <summary> /// 创建一个计算结果为true的谓词。 /// </summary> public static Expression<Func<T, bool>> True<T>() { return param => true; } /// <summary> /// 创建一个计算结果为false的谓词 /// </summary> public static Expression<Func<T, bool>> False<T>() { return param => false; } /// <summary> /// 创建一个从指定的lambda表达式的谓词表达式。 /// </summary> public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; } /// <summary> /// 结合了第二第一谓词使用逻辑“and”。 /// </summary> public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.AndAlso); } /// <summary> /// 结合了第二第一谓词使用逻辑“or”。 /// </summary> public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { return first.Compose(second, Expression.OrElse); } /// <summary> ///否定谓词 /// </summary> public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression) { var negated = Expression.Not(expression.Body); return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters); } /// <summary> /// 使用指定的合并函数,合并第二和第一表达式 /// </summary> static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) { //第二参数映射到第一参数 var map = first.Parameters .Select((f, i) => new { f, s = second.Parameters[i] }) .ToDictionary(p => p.s, p => p.f); //第一lambda表达式的参数替换在第二lambda表达式参数 var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); //从第一个表达式创建一个带参数的合并lambda表达式 return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); } class ParameterRebinder : ExpressionVisitor { readonly Dictionary<ParameterExpression, ParameterExpression> map; ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) { this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); } public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } } }

MyDbContext.CS代码:

public class MyDbContext : DbContext { public MyDbContext() : base("DefaultConnection") { } public DbSet<Customer> Customers { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Customer>().Property(c => c.CompanyName).HasMaxLength(40); modelBuilder.Entity<Customer>().Property(c => c.ContactTitle).HasMaxLength(40); } }

HomeController.cs控制器:

public class HomeController : Controller { public ActionResult Index() { var model = new CustomersViewModel() { Customers = null, PagingInfo = new PagingInfo() { CurrentPage=1, ItemsPerPage= 10, PageOptions = new List<int>() { 10,25, 50, 100}, ShowPageOptions= true, TotalItems=1 } }; return View(model); } public ActionResult GetCustomers(PagingInfo PagingData) { var db = new MyDbContext(); var model = GridViewModelProvider.GetCustomersViewModel(db, PagingData); return PartialView("_CustomersPartial", model); } public ActionResult About() { ViewBag.Message = "您的应用程序描述页面。"; return View(); } public ActionResult Contact() { ViewBag.Message = "您的联系页面。"; return View(); } }

Home视图文件夹下:

_CustomersPartial.cshtml

@model mesoft.gridview.Models.CustomersViewModel @*在这里写下返回的数据的详细信息*@ <span>@Html.Raw(Model.JsonPagingInfo)</span> <table> <thead> <tr> <th> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().Id)) </th> <th data-sort="CompanyName"> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().CompanyName)) </th> <th data-sort="ContactTitle"> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().ContactTitle)) </th> <th> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().Country)) </th> <th> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().City)) </th> <th> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().Address)) </th> <th> @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().Phone)) </th> <th> @Html.DisplayNameFor(x=> x.Customers.FirstOrDefault().Founded) </th> </tr> </thead> <tbody> @if (Model.Customers.Any()) { foreach (var c in Model.Customers) { <tr> <td>@c.Id</td> <td>@c.CompanyName</td> <td>@c.ContactTitle</td> <td>@c.Country</td> <td>@c.City</td> <td>@c.Address</td> <td>@c.Phone</td> <td>@c.Founded</td> </tr> } } else { <tr> <td colspan="8"> <div> <button type="button" data-dismiss="alert" aria-hidden="true">×</button> <strong>警告!</strong> 没有客户找到! </div> </td> </tr> } @Html.Raw(ViewBag.Script) </tbody> </table>

_GridViewPartial.cshtml

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyzdd.html