首先实体是:
----------------------------------------------------------------------------------------------------------------------------------------------------------
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 >
<h4>EmployeeMaster</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div >
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
</div>
</div>
<div >
<div >
<input type="submit" value="Create" />
</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" >
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方法的两个参数,大小写无所谓
原标题:APS.NET MVC 分页和排序,自己练习
关键词:.NET