title: Custom paging logic in MVC 4 view with Stored procedures
body: <p>I am returning records from a stored procedure and I want to use custom paging in view. This is what I have so far:</p>
  
  <p>Controller:</p>
  
  <pre><code>        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&lt;int&gt; 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 &gt; 0)
              sb1.AppendLine(String.Format("&lt;a href=\"{0}/{1}\"&gt;Previous&lt;/a&gt;", SearchString, currentPage));
  
          if (currentPage - PageSize &gt;= 0)
              sb1.AppendLine(String.Format("&lt;a href=\"{0}/{1}\"&gt;...&lt;/a&gt;", SearchString, (currentPage - PageSize) + 1));
  
          for (int i = seed; i &lt; Math.Round((totalRecords / 10) + 0.5) &amp;&amp; i &lt; seed + PageSize; i++)
          {
              sb1.AppendLine(String.Format("&lt;a href=\"{0}/{1}\"&gt;{1}&lt;/a&gt;", SearchString, i + 1));
          }
  
          if (currentPage + PageSize &lt;= (Math.Round((totalRecords / 10) + 0.5) - 1))
              sb1.AppendLine(String.Format("&lt;a href=\"{0}/{1}\"&gt;...&lt;/a&gt;", SearchString, (currentPage + PageSize) + 1));
  
          if (currentPage &lt; (Math.Round((totalRecords / 10) + 0.5) - 1))
              sb1.AppendLine(String.Format("&lt;a href=\"{0}/{1}\"&gt;Next&lt;/a&gt;", SearchString, currentPage + 2));
  
          //Response.Write(sb1);////can/should I append this to the model?
  
          return View(model);
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model IEnumerable&lt;ArtGuildMVC2.Models.TempItemSubClassListResult&gt;
  
  @{
  ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
  @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table&gt;
  &lt;tr&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.ItemClassId)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Description)
      &lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
  &lt;/tr&gt;
  
  @foreach (var item in Model) {
     &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.ItemClassId)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Description)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
          @Html.ActionLink("Details", "Details", new { id=item.Id }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.Id })
      &lt;/td&gt;
  &lt;/tr&gt;
   }
   &lt;/table&gt;
  
  /////What do I do hereafter????
  
   @{
   if (ViewBag.currentPage &gt; 1) {
  &lt;a href="@Url.Action("index",new {page=1})"&gt;First&lt;/a&gt;
  &lt;a href="@Url.Action("index",new {page=ViewBag.currentPage-1})"&gt;Prev&lt;/a&gt;
  }
  if (ViewBag.currentPage &lt; ViewBag.Total)
  {
   &lt;a href="@Url.Action("index",new {page=ViewBag.currentPage+1})"&gt;Next&lt;/a&gt;
   &lt;a href="@Url.Action("index",new {page=ViewBag.Total})"&gt;Last&lt;/a&gt;
  }}
  </code></pre>
  
  <p>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 &amp; error basis.</p>