BootstrapMVC+Bootstrap+Drapper使用PagedList.Mvc扶助多询问条件分页

前些天做3个小小小项目,使用了MVC+Bootstrap,在此此前做分页都以异步加载Mvc部分视图的主意,因为这一个是小项目,就随意一点。一般的列表页面,少不了有查询条件,上面分享下Drapper+PagedList.Mvc帮忙多询问条件分页的应用经验。

营造强类型Model

一.查询参数的Model

    public class OrderQueryParamModel
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string CustomerName { get; set; }
    }

贰.Orders分页数据Model

PagedList提供了一个StaticPagedList<T>泛型类来封装数据。(看看StaticPagedList的源代码,使用10分便利,把T类型的数码subset,pageNumber,pageSize,totalCount起头化进去就可以了。

 public StaticPagedList(IEnumerable<T> subset, IPagedList metaData) : this(subset, metaData.PageNumber, metaData.PageSize, metaData.TotalItemCount)
        {
        }

三.Orders显得页面全体Model

    public class OrderViewModel
    {
        public OrderQueryParamModel QueryModel { get; set; }

        public PagedList.StaticPagedList<OrderModel> OrderList { get; set; } 
    }

OK,接下去看看在Controller中哪些给来OrderViewModel填充数据吧

        public ActionResult List(OrderViewModel orderViewModel, int page = 1)
        {
            var pagesize = 10;
            var count = 0;

            var orders = _orderService.GetOrders(page, pagesize, orderViewModel.QueryModel, ref count);

            orderViewModel.OrderList = new StaticPagedList<OrderModel>(orders, page, pagesize, count);

            return View(orderViewModel);
        }

Controller中代码很轻巧,接收POST过来的多个参数,orderViewModel:包蕴查询参数Model,page:PagedList定义的当前页。

附带看看GetOrders()那些主意吗,为了便利懒得写存款和储蓄进度,直接用了Drapper的QueryMultiple,认为很庞大啊。

        public List<OrderModel> GetOrders(int pageindex, int pagesize, OrderQueryParamModel query, ref int count)
        {
            var orders = new List<OrderModel>();
            var whereStr = string.Empty;

            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.CustomerName))
                {
                    whereStr += string.Format(" and CustomerName like '%{0}%' ", query.CustomerName);
                }
            }

            var cmd = string.Format(@"SELECT COUNT(*) FROM [Orders] WHERE 1=1 {0};
                        SELECT *  FROM (
                        SELECT *, row_number() OVER (ORDER BY orderId DESC ) AS [row] 
                                  FROM [Orders] WHERE 1=1  {0} )t
                        WHERE t.row >@indexMin AND t.row<=@indexMax", whereStr);

            using (IDbConnection conn = BaseDBHelper.GetConn())
            {
                using (var multi = conn.QueryMultiple(cmd, 
                   new { indexMin = (pageindex - 1) * pagesize, indexMax = pageindex * pagesize }))
                {
                    count = multi.Read<int>().SingleOrDefault();
                    orders = multi.Read<OrderModel>().ToList();
                }
            }
            return orders;
        }

此间要留心下的是,multi.Read的相继必须和Sql查询出来的多少集结顺序同样。

前端数据彰显

1.先是在View中加多引用

@using PagedList.Mvc;
@using PagedList;
@model Models.OrderViewModel 

贰.为查询创造3个表单

<div class="page-header">

    @using (Html.BeginForm("List", "Order", FormMethod.Post, new { id = "OrderForm", @class = "form-horizontal" }))
    {
        @Html.Raw("客户名称:") @Html.TextBoxFor(m => m.QueryModel.CustomerName)
        @Html.Raw("订单编号:") @Html.TextBoxFor(m => m.QueryModel.OrderNo)

        <button type="submit" class="btn btn-purple btn-sm">查询</button>

        //咿,这个干吗用的?后面会解释
        <input type="hidden" name="page" value="1" />
    }
</div>

三.绑定数据

<table class="table loading table-bordered margin-top-5 margin-bottom-5">
    <thead>
        <tr>
            <th>订单编号</th>
            <th>客户名称</th>
            <th>手机号码</th>            
            <th>商品数量</th>
            <th>订单金额</th>
            <th>下单时间</th>
        </tr>
    </thead>
    <tbody>

        @foreach (var item in Model.OrderList)
        {
            <tr>
                <td>@item.orderNo</td>
                <td>@item.customerName</td>
                <td>@item.customerMobile</td>
                <td>@item.productQuantity</td>
                <td>@item.orderAmount</td>
                <td>@item.orderCreateTime</td>
            </tr>
        }
    </tbody>
</table>

四.绑定分页插件数据

@if (Model.OrderList != null&&Model.OrderList.Any())
{
    <div class="pagedList" style="margin:0 auto;text-align:center">
        @Html.PagedListPager(Model.OrderList, page => Url.Action("List", new { page }), PagedListRenderOptions.Classic)
    </div>
}

 

OK,壹切消除了,运行你会意识,分页导航生成的链接都以 “/Order/List/2”
那种样式,不能够支撑我们把别的查询参数也传递到Controller。

我们换多个思路,为啥不把page这几个参数放到form表单去了?
 还记得我们form中有三个name=page 的hidden input吧?

    $(function () {

        $(".pagination > li > a").click(function () {

            event.preventDefault();

            var index = $(this).html();

            if (index == '»') {
                index = parseInt($(".pagination > li[class=active] > a").html()) + 1;
            }
            if (index == '«') {
                index = parseInt($(".pagination > li[class=active] > a").html()) - 1;
            }

            if (index < 1) return;

            $("input[name=page]").val(index);
            $("#OrderForm").submit();
        });

    });

 

通过那段JS,直接把原来分页的a标签作废了,获取她的page值放到了form中,然后径直触发form的submit(),那样就满意了大家一般的查询专门的职业要求。 

本文和讯地址:http://www.cnblogs.com/struggle999/p/6918524.html

相关文章