我在网上看着写了一个mvc分页 可是 问题出在 调试的时候 起始页是第0页
也就是水 不是从第一页开始的 附上代码
controller
public ActionResult Index(int? page)
{
var NewsList = DCDC.News.Select(newss => newss);
var paginatedNews = new NewList<News>(NewsList, page ?? 1, 5);//实现分页功能
return View(paginatedNews);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formValues)
{
int? index = int.Parse(formValues.GetValue("pageindex").AttemptedValue);
int page = index ?? 0;
var NewsList = DCDC.News.Select(newss => newss);
var paginatedNews = new NewList<News>(NewsList, page, 5);//实现分页功能
return View(paginatedNews);
}
NewList 类
amespace MvcCompany.Models
{
public class NewList<T> : List<T>
{
/// <summary>
/// 页面索引值
/// </summary>
public int PageIndex { get; private set; }
/// <summary>
/// 每页记录的数量
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 记录总条数
/// </summary>
public int TotalCount { get; private set; }
/// <summary>
/// 共有的页数和
/// </summary>
public int TotalPages { get; private set; }
public NewList(IQueryable<T> source, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
TotalCount = source.Count();
// 进上去取整( 总记录条数/一面记录的条数)
TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
this.AddRange(source.Skip(pageIndex * pageSize).Take(PageSize));
}
/// <summary>
/// 是否存在前续页
/// </summary>
public bool HasPreviousPage
{
get { return (PageIndex > 0); }
}
/// <summary>
/// 是否存在后续页
/// </summary>
public bool HasNextPage
{
get { return (PageIndex + 1 < TotalPages); }
}
}
}
Index
<%using (Html.BeginForm())
{ %>
<h2>
新闻列表</h2>
<table>
<tr>
<th>
编号
</th>
<th>
题目
</th>
<th>
时间
</th>
<th>
内容
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= item.ID%>
</td>
<td>
<%= Html.Encode(item.title)%>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.datatime))%>
</td>
<td>
<%= Html.Encode(item.contents)%>
</td>
</tr>
<% } %>
</table>
<hr />
<% =Html.ActionLink("首页", "Index", new { page = 0 })%>|
<% if (Model.HasPreviousPage)
{%>
<% =Html.ActionLink("上一页", "Index", new { page = (Model.PageIndex - 1) })%>|
<%} %>
<% if (Model.HasNextPage)
{%>
<% =Html.ActionLink("下一页", "Index", new { page = (Model.PageIndex + 1) })%>|
<%} %>
<% =Html.ActionLink("尾页", "Index", new { page = Model.TotalPages - 1 })%>
<div class="clear"><span style="float:left;width:40%">共<%=Model.TotalPages%>条记录,
页<%=Model.PageIndex%>/<%=Model.TotalPages%></span>
第
<%=Html.TextBox("pageindex", Model.PageIndex, new {style="width:30px;"})%>
页<input
type="submit" value="转到" id="sub" /></div>
应该把它最后拼接的SQL或存储过程贴出来。