控制器代码 绑定数据库使用的是EF操作 分页操作略显草略望见谅
///
/// 获取数据上下文
///
CEducationContext ced = new CEducationContext();
/// <returns></returns>
public ActionResult Index(int pageSize = 10, int pageIndex = 1)
{
var newInfoModel = new ModelPageList()
{
pageIndex = pageIndex,
pageSize = pageSize
};
newInfoModel.pageCount = ced.NewInfor.Count();
newInfoModel.NewInfos = ced.NewInfor.OrderBy(m => m.id).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
return View(newInfoModel);
}
主页面Index.cshtml代码
@model MvcApplication1.Models.ModelPageList
div
table
tr
td编号/td
td标题/td
td内容/td
/tr
@foreach (var item in Model.NewInfos)
{
tr
td @item.id /td
td @item.title /td
td @item.content /td
/tr
}
/table
@Html.Partial(“PageD”, Model)
/div
share文件下的分布视图
@model MvcApplication1.Models.ModelPageList
@{
Html.RenderPartial("Plink", Model, new ViewDataDictionary { { "text", "首页" },
{ "pageIndex", 1 }, { "Inactive", Model.pageIndex == 1 } });
tml.RenderPartial("Plink", Model, new ViewDataDictionary { { "text", "上一页" },
{ "pageIndex", Model.pageIndex - 1 }, { "Inactive", Model.pageIndex == 1 } });
var pageList = Model.pageCount % Model.pageSize;
if (pageList > 0)
{
pageList = Model.pageCount / Model.pageSize + 1;
}
for (int i = 1; i <= pageList; i++)
{
Html.RenderPartial("Plink", Model, new ViewDataDictionary { { "text", i },
{ "pageIndex", i }, { "Inactive", false } });
}
Html.RenderPartial("Plink", Model, new ViewDataDictionary { { "text", "下一页" }
, { "pageIndex", Model.pageIndex + 1 }
, { "Inactive", Model.pageIndex == pageList } });
Html.RenderPartial("Plink", Model, new ViewDataDictionary { { "text", "尾页" },
{ "pageIndex", pageList }, { "Inactive", Model.pageIndex == pageList } });
}
share文件夹下另一分布视图
@model MvcApplication1.Models.ModelPageList
@{
var razorWriter = ViewContext.Writer;
if ((bool)ViewData[“Inactive”])
{
razorWriter.Write(string.Format(“{1}“, “”, ViewData[“text”]));
}
else
{
var routeData = new RouteValueDictionary { { “pageIndex”, ViewData[“pageIndex”].ToString() }, { “pageSize”, Model.pageSize } };
var htmlAttributes = new Dictionary 《string, object》();
var linkMarkup = Html.ActionLink(
ViewData[“text”].ToString(), // Link Text
Html.ViewContext.RouteData.Values[“action”].ToString(), // Action
Html.ViewContext.RouteData.Values[“controller”].ToString(), // Controller
routeData, // Route data
htmlAttributes // HTML attributes to apply to hyperlink
).ToHtmlString();
razorWriter.Write(linkMarkup);
}
}
单独定义一个model类用于定义总页数,页索引,页大小和将实例类放入该对象中
public class ModelPageList
{
public int id { get; set; }
public string title { get; set; }
public string content { get; set; }
public int pageSize { get; set; }
public int pageIndex { get; set; }
public int pageCount { get; set; }
public IEnumerable NewInfos { get; set; }
}
实例类大家可以自己定义
下图是演示结果非常尴尬看起来很挫的样子