我从存储过程返回记录,我想在视图中使用自定义分页。这就是我到目前为止所做的:
控制器:
public ActionResult Index(int currentPage=1, int PageNo = 1, int PageSize = 10, string SortColumn = "Name", string SortOrder = "Asc", string SearchString = "", int totalRecords=0)
{
DataContextDataContext obj = new DataContextDataContext();
System.Nullable<int> Total = null;
//PageCount = (int)Math.Ceiling((double)Total / PageSize);
var model = obj.TempItemSubClassList(PageNo, PageSize, SortColumn, SortOrder, SearchString, ref Total).ToList();
int PageCount = (int)(Total + PageSize - 1) / PageSize;
StringBuilder sb1 = new StringBuilder();
int seed = currentPage % PageSize == 0 ? currentPage : currentPage - (currentPage % PageSize);
if (currentPage > 0)
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Previous</a>", SearchString, currentPage));
if (currentPage - PageSize >= 0)
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", SearchString, (currentPage - PageSize) + 1));
for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + PageSize; i++)
{
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", SearchString, i + 1));
}
if (currentPage + PageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", SearchString, (currentPage + PageSize) + 1));
if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", SearchString, currentPage + 2));
//Response.Write(sb1);////can/should I append this to the model?
return View(model);
}
查看:
@model IEnumerable<ArtGuildMVC2.Models.TempItemSubClassListResult>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemClassId)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemClassId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
/////What do I do hereafter????
@{
if (ViewBag.currentPage > 1) {
<a href="@Url.Action("index",new {page=1})">First</a>
<a href="@Url.Action("index",new {page=ViewBag.currentPage-1})">Prev</a>
}
if (ViewBag.currentPage < ViewBag.Total)
{
<a href="@Url.Action("index",new {page=ViewBag.currentPage+1})">Next</a>
<a href="@Url.Action("index",new {page=ViewBag.Total})">Last</a>
}}
如何在视图中实现分页逻辑? 提前致谢。 P.S。:你可能没有找到合乎逻辑的代码,因为我已经从2-3个地方拿到了它,并且我试图让它在试用版上工作。错误基础。
答案 0 :(得分:1)
我想说你将通过你在Action方法中编写的代码打破MVC模式。
顺便说一句,Paging是一些像MvcPaging这样的库解决的问题所以,我强烈建议你使用其中一个。