HOW TO DISPLAY 20 RECORDS EACH PAGE IN MVC4?

anil babu

Total Post:19

Points:133
Posted by  anil babu
 1469  View(s)
Ratings:
Rate this:
How can i display for each page 20 records Just like asp.net gridview Pagesize="20" property, 

In the same way i want to display 20 records for each page in MVC4? 

How can i do this? 

@model IEnumerable<Hoda.Models.Products> 

@{ 
ViewBag.Title = "products"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 


<p> 

@Html.ActionLink("Create", "Create") 



</p> 
<table class="table table-striped" style="width:auto"> 


<tr> 
<th> 
@Html.DisplayNameFor(model => model.No) 
</th> 

<th> 
@Html.DisplayNameFor(model => model.ProductName) 
</th> 
<th> 
@Html.DisplayNameFor(model => model.Specifications) 
</th> 
<th> 
@Html.DisplayNameFor(model => model.Quality_ValueAnalysis) 
</th> 
<th> 
@Html.DisplayNameFor(model => model.Aboutus) 
</th> 

<th> 
@Html.DisplayNameFor(model => model.Price) 
</th> 



</tr> 
@if (Model.Count() == 0) 

<tr> 
<td colspan="4">No rows match for this search</td> 
</tr> 

else 


foreach (var item in Model) 

<tr> 
<td> 
@Html.DisplayFor(modelItem => item.No) 
</td> 

<td> 
@Html.DisplayFor(modelItem => item.ProductName) 
</td> 

<td> 
@Html.Raw(item.Specifications) 
</td> 
<td> 
@Html.Raw(item.Quality_ValueAnalysis) 
</td> 
<td> 
@Html.Raw(item.Aboutus) 
</td> 
@*<td> 
@Html.Raw(item.Reviews) 
</td>*@ 
<td> 
@Html.DisplayFor(modelItem => item.Price) 
</td> 



<td> 
@Html.ActionLink("Edit", "Edit", new { id = item.TagNo }) 
@Html.ActionLink("Details", "Newcars", new { id = item.TagNo}) 

</td> 


</tr> 



</table> 
Present i am using to display all records above code, 

cs 

public ActionResult Index() 

var styles = from s in db.demoproducts select s; 
return View(styles); 
  1. Abhishek Singh

    Post:8

    Points:56
    Re: How to display 20 records each page in mvc4?

    here is my code for paging. from this you can implement paging in MVC4 like this 


     public ActionResult Index(int? page)
            {
                //if (User.Identity.IsAuthenticated && Roles.IsUserInRole("Administrator"))
                //{
                var album = db.albums.Include("Genre").Include("Artist").OrderBy(albums => albums.Title);
                if (Request.HttpMethod != "GET")
                {
                    page = 1;
                }
                int pageSize = 10;
                int pageNumber = (page ?? 1);
                return View(album.ToPagedList(pageNumber, pageSize));
                //}
                //else
                //return RedirectToAction("Index", "Account");
            }


    and you have to add some more lines in your View 
    @model PagedList.IPagedList<NorthwindApps.Models.Product> // this is used for implement paging if you are finding error //for this class then you have to install this package from NuGet

    and this is the last line for your final implementation of paging
     <div style="text-align:center">
                        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
                        of @Model.PageCount
                        @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
                    </div>

    i hope its helpful for you

      Modified On Jan-22-2014 06:19:47 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!