title: ASP.NET MVC 3 Razor view Pagination
body: <p>I'm developing a pagination using MVC 3.0 with code below. All good except how do I populate re-generate the pagination numbers when user selects 5 and then clicks on Next button:</p>
  
  <p>1 2 3 4 <strong>5</strong> Next</p>
  
  <p>When user clicks on Next the pagination should look like below:</p>
  
  <p>Prev 2 3 4 5 6 Next</p>
  
  <p>The code look like below</p>
  
  <pre><code>&lt;ul class="pagination-clean"&gt;
      &lt;% if (ViewData.HasPreviousPage)
          { %&gt;
            &lt;li class="previous"&gt;&lt;a href="&lt;%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex - 1).ToString())%&gt;"&gt;Â« Previous&lt;/a&gt;&lt;/li&gt;
       &lt;% }
         else
          { %&gt;
            &lt;li class="previous-off"&gt;Â« Previous&lt;/li&gt;
       &lt;% } %&gt;
  
       &lt;%for (**int page = 1; page &lt;= ViewData.TotalPages; page++**)
          {
          if (page == ViewData.PageIndex)
              { %&gt;
                &lt;li class="active"&gt;&lt;%=page.ToString()%&gt;&lt;/li&gt;
           &lt;% }
          else
              { %&gt;
                &lt;li&gt;&lt;a href="&lt;%=ViewData.PageActionLink.Replace("%7Bpage%7D", page.ToString())%&gt;"&gt;&lt;%=page.ToString()%&gt;&lt;/a&gt;&lt;/li&gt;
           &lt;% }
          } 
  
         if (ViewData.HasNextPage)
              { %&gt;
                &lt;li class="next"&gt;&lt;a href="&lt;%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex + 1).ToString())%&gt;"&gt;Next Â»&lt;/a&gt;&lt;/li&gt;
           &lt;% }
         else
              { %&gt;
                 &lt;li class="next-off"&gt;Next Â»&lt;/li&gt;
           &lt;% } %&gt;
  &lt;/ul&gt; 
  </code></pre>