zoukankan      html  css  js  c++  java
  • asp.net MVC 使用Bootstrap 分页

    Boostrap分页控件比较美观,

    控制器代码:使用仓储模式实现。如果是直接使用DbContext上下文使用更简单。

    public ActionResult Index(int? page,string categoryName,string SearchTitle)
    {
    var categoryNameList = new List<string>(); //可考虑使用hashSet<>
    var categoryQuery = from c in _articleService.FindAll()
    select c.Category.CategoryName;
    categoryNameList.AddRange(categoryQuery.Distinct());
    ViewBag.categoryNameList = new SelectList(categoryNameList, categoryName);
    ViewBag.categoryName = categoryName;

    ViewBag.SearchTitle = SearchTitle;

    int pageNumber = page ?? 1;  //如果page  参数没有赋值,默认为1。??空值合并运算符,意思是如果page有值,就为page的值,如果page 为空,值就为1.

    ViewBag.page = pageNumber;

    int recordPerPage =3;    //设置每一页记录数。

    ViewBag.recordPerPage = recordPerPage;
    int totalRecord = 0;   //定义一个输出参数。其实这里给输出参数赋值为0,是没多大意义的。只是为了能够访问到这个变量名而已。输出参数就像引用参数一样,跟形参占用相同的内存空间,形参值改变了,输出参数值也变了。 输出参数是为了解决一个C#中一个函数只能返回一个值的问题,使用它能返回多个值。
    IEnumerable<Article> articles;
    if (string.IsNullOrEmpty(categoryName))
    {
    if(string.IsNullOrEmpty(SearchTitle))
    articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a =>true, OrderType.Desc, a => a.PostTime);
    else
    articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a => a.Title.Contains(SearchTitle), OrderType.Desc, a => a.PostTime);
    }
    else
    {
    if (string.IsNullOrEmpty(SearchTitle))
    articles = _articleService.FindPageList<DateTime>(pageNumber,recordPerPage,out totalRecord,a=>a.Category.CategoryName == categoryName,OrderType.Desc,a =>a.PostTime);
    else
    articles = _articleService.FindPageList<DateTime>(pageNumber, recordPerPage, out totalRecord, a => a.Category.CategoryName == categoryName && a.Title.Contains(SearchTitle), OrderType.Desc, a => a.PostTime);

    }


    ViewBag.totalRecord = totalRecord;
    ViewBag.totalPage =(int)Math.Ceiling((double)totalRecord/(double)recordPerPage);      //Math.Celling()向上取整函数。 同样,Math.Floor()向下取整函数。

    return View(articles.ToList());
    }

    视图:

    @model IEnumerable<MajorConstruction.Models.Article>

    @{
    ViewBag.Title = "文章列表";
    }

    <h2>@ViewBag.Title</h2>

    @using (Html.BeginForm("Index", "Article", FormMethod.Get, new { @class = "form-inline", role = "form" }))   //内联表单,显示在一行上。
    {
    <label for="categoryName" class="control-label">栏目名称:</label>
    <div class="form-group">
    @Html.DropDownList("categoryName", ViewBag.categoryNameList as SelectList, "全部栏目", new { @class="form-control"})
    </div>


    <label for="searchTextbox" class="control-label"> 通过标题查找:</label>
    <div class="form-group">
    @Html.TextBox("SearchTitle", ViewBag.SearchTitle as string, new { @class = "form-control" })
    </div>

    <input type="submit" value="查找" class="btn btn-primary" />


    }
    <table class="table table-striped table-hover">
    <thead>
    <tr>
    <th>
    @Html.DisplayNameFor(model => model.Category.CategoryName)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.Title)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.AuthorName)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.PostTime)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.PriorOrder)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.ClickCount)
    </th>
    <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
    <tr>
    <td>
    @Html.DisplayFor(modelItem => item.Category.CategoryName)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.AuthorName)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.PostTime)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.PriorOrder)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.ClickCount)
    </td>


    <td>
    @Html.ActionLink("编辑", "Edit", new { id = item.ArticleID }) |
    @Html.ActionLink("预览", "Details", new { id = item.ArticleID }) |
    @Html.ActionLink("删除", "Delete", new { id = item.ArticleID })
    </td>
    </tr>
    }
    </tbody>
    <tfoot>
    <tr>
    <td class="text-info" colspan="5">
    每页 @ViewBag.recordPerPage 条记录,共有 @ViewBag.totalRecord 条记录。 第 @(ViewBag.totalRecord == 0 ? 0 : ViewBag.page) 页 ,共 @ViewBag.totalPage 页  //如果查询到的记录数为0,就显示为第0页。这里有一个条件表达式的目的是为了避免 如  第1页,共0页。的情况。
    </td>
    </tr>
    </tfoot>
    </table>

    @if (ViewBag.totalRecord != 0)       //是为了避免出现没有记录,还是显示下一页的符号链接。
    {
    <ul class="pagination">
    @if (ViewBag.page != 1)  //如果当前页面不是第1页,就显示 <<上一页的符号链接。当前页面是第1页,就不显示<<了。
    {
    <li><a href="@Url.Action("Index", new { categoryName= ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page =(int)(ViewBag.page) -1})">&laquo;</a></li>   //为了保证分页与筛选功能的一致性,所以在链接中增加了路由参数。并将当前值通过ViewBag回传给各个输入表单字段。
    }

    @for (int page = 1; page <= (int)@ViewBag.totalPage; page++)
    {
    string activeCss = page == (int)ViewBag.page ? "active" : null;
    <li class="@activeCss"><a href="@Url.Action("Index", new { categoryName = ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page = page })">@page</a></li>

    }
    @if (ViewBag.page != ViewBag.totalPage)  //如果当前页面不是最后一页了,就显示 >>下一页的符号链接。当前页面是最后一页,就不显示>>了。
    {
    <li><a href="@Url.Action("Index", new { categoryName = ViewBag.categoryName,SearchTitle=ViewBag.SearchTitle, page = (int)(ViewBag.page) + 1 })">&raquo;</a></li>
    }

    </ul>
    }

  • 相关阅读:
    MHA-Failover(GTID,Auto_Position=0)
    PXC中的GTIDs
    MySQL备份与恢复
    MySQL复制框架
    pt-table-checksum检测不出主从差异处理
    MHA-Failover可能遇到的坑
    MHA-手动Failover流程(传统复制&GTID复制)
    MSSQL-SELECT&UPDATE动作要申请的锁
    sybench压测下模拟误truncate数据恢复
    gtid_executed和gtid_purged变量是如何初始化的
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4474589.html
Copyright © 2011-2022 走看看