• .netCore MVC单一条件分页


    直白点,呈上香喷喷的代码,创建AspNetCoreMvcPager类,直接把下面代码复制进去就行

     public static class PagedList<T>
        {
            public static Tuple<List<T>, PagerModel> PageList(int pageindex, int pagesize, List<T> list)
            {
                int count = list.Count();
                int num = (count % pagesize == 0) ? (count / pagesize) : (count / pagesize + 1);
                List<T> list2 = Enumerable.ToList<T>(Enumerable.Take<T>(Enumerable.Skip<T>(list, (pageindex - 1) * pagesize), pagesize));
                PagerModel pagerModel = new PagerModel();
                pagerModel.pageIndex = pageindex;
                pagerModel.pageCount = num;
                pagerModel.Count = count;
                pagerModel.pageStart = ((pageindex - 2 > 0) ? (pageindex - 2) : 1);
                pagerModel.pageEnd = ((pageindex + 2 >= num) ? num : (pagerModel.pageStart + 4));
                return new Tuple<List<T>, PagerModel>(list2, pagerModel);
            }
        }
    
        public static class PagerHtmlString
        {
            public static IHtmlContent Pager(PagerModel model, string controllerName, string actionName, bool Total)
            {
                StringBuilder stringBuilder = new StringBuilder();
    
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='page_first nopage' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    1,
                    "">首页</a>"
                }));
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='prev nopage' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    (model.pageIndex == 1) ? 1 : (model.pageIndex - 1),
                    "">上一页</a>"
                }));
                //stringBuilder.Append("</li>");
                for (int i = model.pageStart; i <= model.pageEnd; i++)
                {
                    //stringBuilder.Append("<li class=" + ((model.pageIndex == i) ? "active" : "") + ">");
                    if (model.pageIndex == i)
                    {
                        stringBuilder.Append(string.Concat(new object[]
                       {
                        "<span style='margin-left:4px; ' class='current " + ((model.pageIndex == i) ? "cur" : "")+"' 'href="/'",
                        controllerName,
                        "/",
                        actionName,
                        "?pageindex=",
                        i,
                        "">",
                        ""+i+"" +
                        "</span>"
                        }));
                    }
                    else
                    {
                        stringBuilder.Append(string.Concat(new object[]
                        {
                        "<a  style='margin-left:4px; ' href="/",
                        controllerName,
                        "/",
                        actionName,
                        "?pageindex=",
                        i,
                        "">",
                        ""+i+"" +
                        "</a>"
                         }));
                     }
                }
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='next' style='margin-left:4px; ' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    (model.pageIndex == model.pageCount) ? model.pageCount : (model.pageIndex + 1),
                    "">下一页</a>"
                }));
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='page_last' style='margin-left:4px; ' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    model.pageCount,
                    "">尾页</a>"
                }));
                bool flag = model.Count > 0;
                IHtmlContent result;
                if (flag)
                {
                    result = new HtmlString(stringBuilder.ToString());
                }
                else
                {
                    result = new HtmlString("");
                }
                return result;
            }
            public static IHtmlContent PagerSearch(PagerModel model, string controllerName, string actionName,  bool Total)
            {
                StringBuilder stringBuilder = new StringBuilder();
                string strSearch = "";
                if (model.search.SerachName!="")
                {
                    strSearch = strSearch + "&SearchName=" + model.search.SerachName;
                }
                if (model.search.rdata != "")
                {
                    strSearch = strSearch + "&rdata=" + model.search.rdata;
                }
                if (model.search.sporttype >0)
                {
                    strSearch = strSearch + "&sporttype=" + model.search.sporttype;
                }
                if (model.search.FileType >0)
                {
                    strSearch = strSearch + "&FileType=" + model.search.FileType;
                }
                if (model.search.SubType>0)
                {
                    strSearch = strSearch + "&SubType=" + model.search.SubType;
                }
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='page_first nopage' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    1,strSearch,
                    "">首页</a>"
                }));
    
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='prev nopage' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    (model.pageIndex == 1) ? 1 : (model.pageIndex - 1),strSearch,
                    "">上一页</a>"
                }));
    
                for (int i = model.pageStart; i <= model.pageEnd; i++)
                {
                    if (model.pageIndex == i)
                    {
                        stringBuilder.Append(string.Concat(new object[]
                       {
                        "<span style='margin-left:4px; ' class='current " + ((model.pageIndex == i) ? "cur" : "")+"' 'href="/'",
                        controllerName,
                        "/",
                        actionName,
                        "?pageindex=",
                        i,strSearch,
                        "">",
                        ""+i+"" +
                        "</span>"
                        }));
                    }
                    else
                    {
                        stringBuilder.Append(string.Concat(new object[]
                       {
                        "<a  style='margin-left:4px; ' href="/",
                        controllerName,
                        "/",
                        actionName,
                        "?pageindex=",
                        i,strSearch,
                        "">",
                        ""+i+"" +
                        "</a>"
                         }));
                      }
                }
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='next' style='margin-left:4px; ' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    (model.pageIndex == model.pageCount) ? model.pageCount : (model.pageIndex + 1),strSearch,
                    "">下一页</a>"
                }));
    
                stringBuilder.Append(string.Concat(new object[]
                {
                    "<a class='page_last' style='margin-left:4px; ' href="/",
                    controllerName,
                    "/",
                    actionName,
                    "?pageindex=",
                    model.pageCount,strSearch,
                    "">尾页</a>"
                }));
                bool flag = model.Count > 0;
                IHtmlContent result;
                if (flag)
                {
                    result = new HtmlString(stringBuilder.ToString());
                }
                else
                {
                    result = new HtmlString("");
                }
                return result;
            }
        }
           
        public class PagerModel
        {
            public int pageIndex
            {
                get;
                set;
            }
    
            public int pageCount
            {
                get;
                set;
            }
    
            public int Count
            {
                get;
                set;
            }
    
            public int pageStart
            {
                get;
                set;
            }
    
            public int pageEnd
            {
                get;
                set;
            }
            public SearchInput search
            {
                get;set;
            }
        }
        //条件类型
        public class SearchInput
        {
            //搜索
            public string SerachName { get; set; }
            //日期
            public string rdata { get; set; }
            //文件类型ID
            public int FileType { get; set; }
            //运动类型ID
            public int sporttype { get; set; }
            //子类ID
            public int SubType { get; set; }
        }
    View Code

    创建完分页类后,直接在 Controller中使用,如下

            using AspNetCoreMvcPager;//引用
            /// <summary>
            /// 活动列表
            /// </summary>
            /// <returns></returns>
            public async Task<IActionResult> StudentList(string SearchName = "",int PageIndex = 1)
            {
                SearchDto search = new SearchDto()
                {
                    PageIndex = PageIndex,//当前页
                    Pagesize = 10//分页数
                };
                //获取数据
                var tactivitiesList = await _tactivitiesManager.GettactivityList();
                var pagedList = PagedList<StudentModel>.PageList(search.PageIndex, search.Pagesize, tactivitiesList);
                //组装参数
                  pagedList.Item2.pageIndex = pageindex;
                  SearchInput input = new SearchInput()
                  {
                    SerachName = SearchName
                  };
                  pagedList.Item2.search = input;
                  ViewBag.model = pagedList.Item2;
    
                return View(pagedList.Item1);
            }
    View Code

    在页面代码如下

    @using AspNetCoreMvcPager;
    <div class="turnpage"> @Html.Raw(PagerHtmlString.Pager(ViewBag.model, "Activities", "ActivityList", true)) </div>

    执行效果图(需要自己写css样式进行控制):

  • 相关阅读:
    获取发布的头条的url,避免点击打开新的页面
    下载图片 保存至本地 返回路径
    线程运行的3个状态
    程序并发执行所需付出的时空开销
    web metrics dashboard 数据分析工具 看板 从可视化发现问题 避免sql重复写 调高效率
    Binary safe
    simple dynamic string
    a
    a
    从业务角度 减少代码执行的时间 和 因长时间执行的而带来的代码复杂性 日志恢复数据
  • 原文地址:https://www.cnblogs.com/xinbaba/p/10342805.html
Copyright © 2020-2023  润新知