首先實體是: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace Pag ...
首先實體是: ---------------------------------------------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace PagingAndSortingInMvc.Entities { public class EmployeeMaster { [Key] public string ID { get; set; } [Required(ErrorMessage="請輸入員工名字")] public string Name { get; set; } [Required(ErrorMessage="請輸入手機號碼")] public string PhoneNumber { get; set; } [Required(ErrorMessage="請輸入Email")] public string Email { get; set; } [Required(ErrorMessage= "請輸入薪水")] public decimal Salary { get; set; } } } ------------------------------------------------------------------------------------------------------------------------------------------------------------ 然後控制器的方法: using PagedList; using PagingAndSortingInMvc.Entities; using PagingAndSortingInMvc.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace PagingAndSortingInMvc.Controllers { public class EmployeeController : Controller { #region 列表分頁展示 /// <summary> /// 列表分頁展示 /// </summary> /// <param name="sortOrder">按照什麼排序</param> /// <param name="currentSort">當前排序是</param> /// <param name="page">第幾頁</param> /// <returns></returns> public ActionResult Index(string sortOrder, string currentSort, int? page) { ApplicationDbContext db = new ApplicationDbContext(); int pageSize = 10; //每頁10條 int pageIndex = 1; //當前頁預設設置為1 pageIndex = page.HasValue ? Convert.ToInt32(page) : 1; //判斷可控對象是否有值 ViewBag.CurrentSort = sortOrder; sortOrder = string.IsNullOrEmpty(sortOrder) ? "Name" : sortOrder; //如果sortOrder為空,就設置為Name,下麵設置的時候,預設按照名稱排序 IPagedList<EmployeeMaster> employees = null; switch (sortOrder) { case "Name": if (sortOrder.Equals(currentSort)) { employees = db.Employees.OrderByDescending(m => m.Name).ToPagedList(pageIndex, pageSize); //降序OrderByDescending } else { employees = db.Employees.OrderBy(m => m.Name).ToPagedList(pageIndex, pageSize); } break; case "PhoneNumber": if (sortOrder.Equals(currentSort)) { employees = db.Employees.OrderByDescending(m => m.PhoneNumber).ToPagedList(pageIndex, pageSize); } else { employees = db.Employees.OrderBy(m => m.PhoneNumber).ToPagedList(pageIndex, pageSize); } break; case "Email": if (sortOrder.Equals(currentSort)) { employees = db.Employees.OrderByDescending(m => m.Email).ToPagedList(pageIndex, pageSize); } else { employees = db.Employees.OrderBy(m => m.Email).ToPagedList(pageIndex, pageSize); } break; case "Salary": if (sortOrder.Equals(currentSort)) { employees = db.Employees.OrderByDescending(m => m.Salary).ToPagedList(pageIndex, pageSize); } else { employees = db.Employees.OrderBy(m => m.Salary).ToPagedList(pageIndex, pageSize); } break; default: if (sortOrder.Equals(currentSort)) { employees = db.Employees.OrderByDescending(m => m.Name).ToPagedList(pageIndex, pageSize); //降序OrderByDescending } else { employees = db.Employees.OrderBy(m => m.Name).ToPagedList(pageIndex, pageSize); } break; } return View(employees); } #endregion /// <summary> /// 添加的思路:首先一個空白的表單,讓用戶輸入,然後點擊點擊,就Post提交到伺服器 /// </summary> /// <returns></returns> public ActionResult Add() { return View(); } [HttpPost] [ValidateAntiForgeryToken] //防止跨站點攻擊需要加的特性標識ValidateAntiForgeryToken public ActionResult Add(EmployeeMaster model) { model.ID = Guid.NewGuid().ToString(); ApplicationDbContext db = new ApplicationDbContext(); db.Employees.Add(model); db.SaveChanges(); return RedirectToAction("Index"); } } } --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 視圖Add:[不是重點] @model PagingAndSortingInMvc.Entities.EmployeeMaster @{ ViewBag.Title = "Add"; } <h2>Add</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>EmployeeMaster</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 視圖Index【重點理解】 @model PagedList.IPagedList<PagingAndSortingInMvc.Entities.EmployeeMaster> @using PagedList.Mvc; <style> table { width: 100%; } table tr td { border: 2px solid black; text-align: center; word-wrap: break-word; } table tr:hover { background-color: #000; color: #fff; } table tr th { border: 2px solid black; text-align: center; background-color: #fff; color: #000; } </style> <h2>Employee List</h2> @using (Html.BeginForm()) { <table> <tr> <th> @Html.ActionLink("Employee Name", "Index", "Employee", 特別註意:這裡的超鏈接,不能帶控制器,帶了,sortOrder 和CurrentSort參數就傳遞不到Action方法 new { sortOrder = "Name", currentSort = ViewBag.CurrentSort }) sortOrder 和currentSort 是對應控制器Index方法的兩個參數,大小寫無所謂 </th> <th> @Html.ActionLink("PhoneNumber", "Index", new { sortOrder = "PhoneNumber", currentSort = ViewBag.CurrentSort }) </th> <th> @Html.ActionLink("Email", "Index", new { sortOrder = "Email", currentSort = ViewBag.CurrentSort }) </th> <th> @Html.ActionLink("Salary", "Index", new { sortOrder = "Salary", currentSort = ViewBag.CurrentSort }) </th> </tr> @foreach (var item in Model) { <tr> <td>@item.Name</td> /// @item.Name和@Html.DisplayFor(m=>item.Email)都可以 <td>@item.PhoneNumber</td> <td>@Html.DisplayFor(m=>item.Email)</td> <td>@Html.DisplayFor(m=>item.Salary)</td> </tr> } </table> <br/> <div id="Paging" style="text-align:center"> Page @(Model.PageCount<Model.PageNumber?0:Model.PageNumber) of @Model.PageCount PageNumber當前頁 @Html.PagedListPager(Model, page => Url.Action("Index", new { page})) </div> } ------------------------------------------------------------------------------------ 擴展: @Html.PagedListPager(Model, page => Url.Action("Index", new { page}),PagedListRenderOptions.ClassicPlusFirstAndLast) @Html.ActionLink("Employee Name", "Index", "Employee", 特別註意:這裡的超鏈接,不能帶控制器,帶了,sortOrder 和CurrentSort參數就傳遞不到Action方法 new { sortOrder = "Name", currentSort = ViewBag.CurrentSort }) sortOrder 和currentSort 是對應控制器Index方法的兩個參數,大小寫無所謂