Home > DeveloperSection > Forums > How to display 20 records each page in mvc4?
anil babu
anil babu

Total Post:19

Points:133
Posted on    January-21-2014 4:49 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 1159  View(s)
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); 


Abhishek Singh

Total Post:8

Points:56
Posted on    January-22-2014 6:15 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick