title: How to create pagination in asp.net mvc
body: <p>I want to make pagination with 5 articles per page using asp.net MVC,
  what code should I add?</p>
  
  <p>Here my controller:</p>
  
  <pre><code>    using CBA.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using CBA.ViewModels;
  using System.Data;
  
  namespace CBA.Controllers
  {
      public class EventController : Controller
      {
          private ModelEntities db = new ModelEntities();
  
         public ActionResult Index()
          //public ActionResult Index(int page = 1, int pageSize 5)
          {
              EventDetailsViewModel eventDetail = new EventDetailsViewModel();
              MasterEvents events = new MasterEvents();
  
              eventDetail.Title = events.Title;
              eventDetail.CreatedTime = events.CreatedTime;
              eventDetail.Detail = events.DetailEvent;
              eventDetail.CreatedBy = events.CreatedBy_Id;
              eventDetail.Description = events.ShortDescription;
  
              CBA.GetContent.GetContentSoapClient service = new CBA.GetContent.GetContentSoapClient();
  
              string[] Content = service.GetContentText("Events", DateTime.Now.ToString("MM-dd-yyyy"), clsEncrypt.EncodeTo64(DateTime.Now.ToString("MM-dd-yyyy")));
              if (Content[0] != null)
              {
                  string id = Content[0];
  
                  string ContentText;
  
                  if (Content[1].Length == 0 &amp;&amp; Content[2].Length == 0 &amp;&amp; Content[3].Length == 0)
                  {
                      ContentText = "";
                  }
                  else
                  {
                      ContentText = "&lt;div class=\"container\" #MARGIN-TOP# &gt;&lt;div class=\"row\"&gt;&lt;div class=\"col-lg-10 col-lg-offset-1\"&gt;";
                      ContentText += "&lt;div class=\"col-lg-12\" style=\"text-align:center;\"&gt;&lt;h1&gt;" + Content[1] + "&lt;/h1&gt;&lt;/div&gt;";
                      ContentText += "&lt;div class=\"col-lg-12\" style=\"text-align:center;\"&gt;&lt;h2&gt;" + Content[2] + "&lt;/h2&gt;&lt;/div&gt;";
                      ContentText += "&lt;div class=\"col-lg-12\"&gt;&lt;div class=\"form-group\"&gt;" + HttpUtility.HtmlDecode(Content[3]).Replace("src=\"/", "src=\"" + System.Configuration.ConfigurationManager.AppSettings["replaceURLImageSlider"] + "/") + "&lt;/div&gt;";
                      ContentText += "&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;";
                  }
  
                  DataTable dtSlider = service.GetContentImageSlider(int.Parse(id), DateTime.Now.ToString("MM-dd-yyyy"), clsEncrypt.EncodeTo64(DateTime.Now.ToString("MM-dd-yyyy")));
  
                  if (dtSlider.Rows.Count &gt; 0)
                  {
  
                      string ContentSlider = "&lt;div id=\"myCarousel\" class=\"carousel slide\"&gt;&lt;ol class=\"carousel-indicators\"&gt;";
  
                      for (int i = 0; i &lt; dtSlider.Rows.Count; i++)
                      {
                          if (i == 0)
                          {
                              ContentSlider += "&lt;li data-target=\"#myCarousel\" data-slide-to=\"" + i.ToString() + "\" class=\"active\"&gt;&lt;/li&gt;";
                          }
                          else
                          {
                              ContentSlider += "&lt;li data-target=\"#myCarousel\" data-slide-to=\"" + i.ToString() + "\"&gt;&lt;/li&gt;";
                          }
                      }
  
                      ContentSlider += "&lt;/ol&gt;&lt;div class=\"carousel-inner\"&gt;";
  
                      for (int i = 0; i &lt; dtSlider.Rows.Count; i++)
                      {
                          //dt.Columns.Add("ImageFile", typeof(string));
                          //dt.Columns.Add("HeaderText", typeof(string));
                          //dt.Columns.Add("ContentText", typeof(string));
  
                          string img = dtSlider.Rows[i]["ImageFile"].ToString().Replace("..", System.Configuration.ConfigurationManager.AppSettings["replaceURLImageSlider"]);
  
                          if (i == 0)
                          {
                              ContentSlider +=
                                  "&lt;div class=\"item active\" style=\"background:url('" + dtSlider.Rows[i]["ImageFile"].ToString().Replace("..", System.Configuration.ConfigurationManager.AppSettings["replaceURLImageSlider"]) + "') "
                                  + "no-repeat center center; -webkit-background-size: cover; -moz-background-size: cover; "
                                  + "-o-background-size: cover;background-size: cover;\"&gt;"
                                      + "&lt;div class=\"container\"&gt;"
                                          + "&lt;div class=\"carousel-caption\"&gt;"
                                              + "&lt;div class=\"col-lg-6\" style='width:260px;'&gt;"
                                                  + "&lt;h1&gt;" + (string)dtSlider.Rows[i]["HeaderText"] + "&lt;/h1&gt;"
                                                  + "&lt;p class=\"scroll_bni\" style='width: 260px;'&gt;"
                                                      + (string)dtSlider.Rows[i]["ContentText"]
                                                  + "&lt;/p&gt;"
                                              + "&lt;/div&gt;"
                                          + "&lt;/div&gt;"
                                      + "&lt;/div&gt;"
                                  + "&lt;/div&gt;";
                          }
                          else
                          {
                              ContentSlider +=
                                     "&lt;div class=\"item\" style=\"background:url('" + dtSlider.Rows[i]["ImageFile"].ToString().Replace("..", System.Configuration.ConfigurationManager.AppSettings["replaceURLImageSlider"]) + "') "
                                     + "no-repeat center center; -webkit-background-size: cover; -moz-background-size: cover; "
                                     + "-o-background-size: cover;background-size: cover;\"&gt;"
                                         + "&lt;div class=\"container\"&gt;"
                                             + "&lt;div class=\"carousel-caption\"&gt;"
                                                 + "&lt;div class=\"col-lg-6\" style='width:260px;'&gt;"
                                                     + "&lt;h1&gt;" + (string)dtSlider.Rows[i]["HeaderText"] + "&lt;/h1&gt;"
                                                     + "&lt;p class=\"scroll_bni\" style='width: 260px;'&gt;"
                                                         + (string)dtSlider.Rows[i]["ContentText"]
                                                     + "&lt;/p&gt;"
                                                 + "&lt;/div&gt;"
                                             + "&lt;/div&gt;"
                                         + "&lt;/div&gt;"
                                     + "&lt;/div&gt;";
                          }
                      }
  
                      ContentSlider += "&lt;/div&gt;&lt;a class=\"left carousel-control\" href=\"#myCarousel\" data-slide=\"prev\"&gt;&lt;span class=\"glyphicon glyphicon-chevron-left\"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a class=\"right carousel-control\" href=\"#myCarousel\" data-slide=\"next\"&gt;&lt;span class=\"glyphicon glyphicon-chevron-right\"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;";
  
                      ViewBag.Carousel = ContentSlider;
                  }
                  else
                  {
                      ContentText = ContentText.Replace("#MARGIN-TOP#", "style=\"margin-top:60px;\"");
                  }
  
                  ContentText = ContentText.Replace("#MARGIN-TOP#", "");
  
                  ViewBag.Content = ContentText;
              }
  
              return View(db.MasterEvents.ToList());
          }
  
  
          public ActionResult ViewEvents(int id)
          {
              MasterEvents MasterEvents = db.MasterEvents.Find(id);
              ViewBag.data = id;
              return View();
          }
  }
  }
  </code></pre>
  
  <p>Here my view:</p>
  
  <pre><code> @model IEnumerable&lt;CBA.Models.MasterEvents&gt;
  
  @{
      ViewBag.Title = "Recruitment - DDR Events";
      ViewBag.lnkEvents = "active";
      Layout = "~/Views/Shared/ContentFrontEnd.cshtml";
  }
  
  &lt;!-- Carousel
      ================================================== --&gt;
  @Html.Raw(ViewBag.Carousel)
  &lt;!-- /.carousel --&gt;
  &lt;!-- Content
      ================================================== --&gt;
  @Html.Raw(ViewBag.Content)
  &lt;!-- /.Content --&gt;
  
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="container" style="background-color: white; border-radius: 0 0 15px 15px; margin-bottom: 40px;"&gt;
          &lt;div class="row" style="margin-top: 30px;"&gt;
              &lt;div class="col-lg-10 col-lg-offset-1"&gt;
                  @foreach (var item in Model)
                  {
                      &lt;div class="blog-post"&gt;
                          &lt;h2 class="blog-post-title"&gt;@Html.DisplayFor(modelItem =&gt; item.Title)&lt;/h2&gt;
                          &lt;p class="blog-post-meta"&gt;
                              Created Time @Html.DisplayFor(modelItem =&gt; item.CreatedTime)
                              &lt;!--by &lt;a href="#"&gt;@Html.DisplayFor(modelItem =&gt; item.CreatedBy_Id)&lt;/a&gt;&lt;/p&gt;--&gt;
                              &lt;h2 class="blog-post-title"&gt;@Html.DisplayFor(modelItem =&gt; item.ShortDescription)&lt;/h2&gt;
                              &lt;div class="readmore"&gt;
                            &lt;p&gt;
  @{
                              string parameterValue = "";
                              if (item.DetailEvent.ToString().Length &lt; 100)
                              {
                                 parameterValue = item.DetailEvent; 
                              }
                              else
                              {
                                  parameterValue = item.DetailEvent.ToString().Substring(0, 200); 
  
  
                              }
                              }
   @Html.Raw(parameterValue);
                                  &lt;/p&gt;
                              &lt;/div&gt;
                              &lt;br /&gt;
   @Html.ActionLink("Read More..", "ViewEvents", "Event", new { id = 7 }, new { @class = "btn btn-primary btn-lg", style = "width:180px;" })
  
                          &lt;/p&gt;
                           &lt;p class="blog-post-meta"&gt;
                              Update Time @Html.DisplayFor(modelItem =&gt; item.UpdatedTime)
                          &lt;p&gt;
                          &lt;/div&gt; 
                  }
                  &lt;nav&gt;
                      &lt;ul class="pager"&gt;
                          &lt;li&gt;&lt;a href="#"&gt;Previous&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#"&gt;Next&lt;/a&gt;&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/nav&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here my models:</p>
  
  <pre><code>using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Web;
  
      namespace CBA.Models
      {
          public class MasterEvents
          {
              [Key]
              [DisplayName("ID")]
              [ScaffoldColumn(false)]
              public int Id { get; set; }
  
              [Required(ErrorMessage = "Required Events Title")]
              [DisplayName("Title")]
              [StringLength(250)]
              public string Title { get; set; }
  
              [Required(ErrorMessage = "Required Short Description")]
              [DisplayName("Short Description")]
              [StringLength(250)]
              public string ShortDescription { get; set; }
  
              [Required(ErrorMessage = "Required Details")]
              [DisplayName("Detail Events")]
              [StringLength(20)]
              public string DetailEvent { get; set; }
  
              [DisplayName("Created Time")]
              public System.DateTime? CreatedTime { get; set; }
  
              [DisplayName("Updated Time")]
              public System.DateTime? UpdatedTime { get; set; }
  
              [DisplayName("Created By")]
              public int? CreatedBy_Id { get; set; }
  
              [DisplayName("Updated By")]
              public int? UpdatedBy_Id { get; set; }
  
  
          }
      }
  </code></pre>
  
  <p>How to make pagination, I want to make pagination with 5 articles per page..</p>