I am returning records from a stored procedure and I want to use custom paging in view. This is what I have so far:
Controller:
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);
}
View:
@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>
}}
How do I implement the paging logic in view? Thanks in advance. P.S.: You may not find the code very logical since I have picked it up from 2-3 places and am trying to make it work on trial & error basis.
I would say that you are going to break MVC pattern by the code that you have written in your Action method.
By the way, Paging is a solved problem by some libraries like MvcPaging so, I strongly recommend you using one of them.