• 符合mvc思维的分页思想


    1.Model Student.cs
    namespace WebApplication14.Models
    {
        public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
    
    
    2.views Index.cshtml
    
    
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    @model WebApplication14.Models.Student
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
    
            <table id="table1">
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>年龄</th>
    
                </tr>
                @foreach (var stu in ViewData["stus"] as List<WebApplication14.Models.Student>)
                {
                    <tr>
                        <td>@stu.Id</td>
                        <td>@stu.Name</td>
                        <td>@stu.Age</td>
                    </tr>
    
     
    
                }
            </table>
            @MvcHtmlString.Create(ViewData["pager"] as string);
    
     
    
     
    
            </table>
        </div>
    </body>
    </html>
    
    3.控制器代码  Controllers
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using WebApplication14.Models;
    
    namespace WebApplication14.Controllers
    {
        public class HomeController : Controller
        {
            public List<Student> GetStudents()
            {
                List<Student> stus = new List<Student>();
                for (int i = 1; i <= 58; i++)
                {
                    stus.Add(new Student() { Id = i, Name = i.ToString(), Age = i });
                }
                return stus;
            }
            [HttpGet]
            public ActionResult Index()
            {
    
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                int totalCount = GetStudents().Count;
                int pageSize = 3;
                List<Student> stus = GetStudents().OrderBy(x => x.Id).Take(3).ToList();
                if (ViewData["currentPage"] == null)
                {
                    ViewData["currentPage"] = 1;
                }
                int currentPage = 1;
                int totalPage = (totalCount - 1) / pageSize + 1;
                ViewData["stus"] = stus;
                int pages = 5;
                int end = currentPage + pages - 1 > totalPage ? totalPage : currentPage + pages - 1;
                for (int i = currentPage; i <=end ; i++)
                {
                    sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>{5}</a>", i.ToString(),3,totalCount,totalPage,pages, i.ToString()));
    
                }
                sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>上一页</a>",currentPage-1>=1?currentPage-1:1,pageSize,totalCount,totalPage,pages));
                sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>下一页</a>", currentPage +1>totalPage?totalPage:currentPage+1 ,pageSize, totalCount,totalPage,pages));
                ViewData["pager"] = sb.ToString();
                return View();
              
            }
     
            public ActionResult ProcessPaging(int pageIndex,int pageSize,int totalCount,int totalPage,int pages)
            {
                var stus = GetStudents();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                ViewData["stus"] = stus.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                int end = pageIndex + pages - 1 > totalPage ? totalPage : pageIndex + pages - 1;
                for (int i = 0; i < 2*pages+1; i++)
                {
                    if (pageIndex - pages + i >= 1 && pageIndex - pages + i <= totalPage)
                    {
                        int cur = pageIndex - pages + i;
                        if (cur == pageIndex)
                        {
                            sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'><font color='red'>{5}</font></a>", cur.ToString(), pageSize, totalCount, totalPage, pages, cur.ToString()));
                        }
                        else
                        {
                            sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>{5}</a>", cur.ToString(), pageSize, totalCount, totalPage, pages, cur.ToString()));
                        }
                    }
                }
         
                sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>上一页</a>", pageIndex - 1 >= 1 ? pageIndex - 1 : 1, pageSize, totalCount, totalPage, pages));
                sb.Append(string.Format("<a href='/Home/ProcessPaging?pageIndex={0}&pageSize={1}&totalCount={2}&totalPage={3}&pages={4}'>下一页</a>", pageIndex + 1 > totalPage ? totalPage : pageIndex + 1, pageSize, totalCount, totalPage, pages));
                ViewData["pager"] = sb.ToString();
                return View("Index");
            }
    
        
         
        }
    }
  • 相关阅读:
    SQL 使用identity(int,1,1)来产生行号。
    SQL DateName\DatePart 返回表示指定date的指定datepart的字符串
    让我们受用一生的好习惯
    SCRUM软件开发过程(转)
    计算机英语词汇
    oral English英语绕口令(转)
    Setup相关经验总结
    与老外吵架之必会109句
    BAT批处理文件语法(转)
    SQL Server 2005之PIVOT/UNPIVOT行列转换(转)
  • 原文地址:https://www.cnblogs.com/kexb/p/5211712.html
Copyright © 2020-2023  润新知