今天晚上搞了些简单的东西,但是耗时也不短啊,自己还需不断的敲代码啊.
在做mvc的分页的时候,首先写了一个给html扩展一个分页的方法:如下(这个分页算法是抄的)
public static HtmlString ShowPage(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
{
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == 0 ? 3 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
var output = new StringBuilder();
if (totalPages > 1)
{
if (currentPage != 1)
{//处理首页连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
}
if (currentPage > 1)
{//处理上一页的连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
}
else
{
// output.Append("<span class='pageLink'>上一页</span>");
}
output.Append(" ");
int currint = 5;
for (int i = 0; i <= 10; i++)
{//一共最多显示10个页码,前面5个,后面5个
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{//当前页处理
//output.Append(string.Format("[{0}]", currentPage));
output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage , pageSize, currentPage );
}
else
{//一般页处理
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
}
}
output.Append(" ");
}
if (currentPage < totalPages) //处理下一页的链接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo,
currentPage + 1, pageSize);
else
{
//output.Append("<span class='pageLink'>下一页</span>");
}
output.Append(" ");
if (currentPage != totalPages)
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo,
totalPages, pageSize);
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
return new HtmlString(output.ToString());
}
在Controller里面接受处理请求的参数,请做相应的分页
ViewBag.pageIndex = Request.QueryString["pageIndex"] == null ? 1 : int.Parse(Request.QueryString["pageIndex"]);
ViewBag.pageSize = Request.QueryString["pageSize"] == null ? 5 : int.Parse(Request.QueryString["pageSize"]);
ViewBag.pageCount = db.Branch.Count();
var list = db.Branch.OrderBy(w => w.ID).Skip((int)ViewBag.pageSize * ((int)ViewBag.pageIndex - 1)).Take((int)ViewBag.pageSize);
return View(list);
在前台页面直接输出显示
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>序号</th> <th>名字</th> <th>年龄</th>
</tr>
@{
foreach (var item in Model)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Age</td>
</tr>
}
}
</table>
接下来还做个简单的微软的异步
形似:@using(@Ajax.BeginForm()){};具体代码如下:
@Html.ShowPage((int)ViewBag.pageIndex,(int)ViewBag.pageSize,(int)ViewBag.pageCount);
<div>
@using (Ajax.BeginForm("GetDate", "TestPage",
new AjaxOptions
{
Confirm = "确定吗?",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result",
HttpMethod = "post",
OnSuccess = "afterSuccess"
}))
{
<input type="submit" name="name" value="提交" />}
</div>
<div id="result">
</div>
这些都是很简单,本来说快速过一遍,结果出现很多奇葩的问题,之前把Resharper卸载了,结果vs自带错误的提示都没有了,郁闷。
11点了,睡了!!