Asp.NET MVC Pagination Entity Framework (Sayfalama - PageList)
<p><strong>Install-Package PagedList</strong></p> <p><strong>Install-Package PagedList.Mvc</strong></p> <p>Solution Explorer penceresinden Controller sağ tıklayıp <strong>Add>Controller> MVC 5 Empty controller</strong> ekleyin. Aşağıdaki kodları yazınız.</p> <pre> <code>public class ProductController : Controller { private MvcTestEntities db = new MvcTestEntities(); public ActionResult Index(int page=1,int pageSize=2) { //veritabanından products tablosunu çekiyoruz. List<Product> products = db.Products.ToList(); //Burada ise PageList tipinde bir Product model oluşturuyoruz ve özelliklerini veriyoruz. Sayfa sayısı ve sayfada kaç tane kayıt gösterilecek gibi.. PagedList<Product> model = new PagedList<Product>(products, page, pageSize); return View(model); } } </code></pre> <p>Son olarak ta <strong>Views>Product</strong> Klasörün içerisine bir tane <strong>Index.cshtml</strong> oluşturunuz.</p> <pre> <code>@{ Layout = null; } @model PagedList.IPagedList<PaginationMVCApp.Models.Product> @using PagedList.Mvc <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Pagination</title> <link rel="stylesheet" href="<a href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="nofollow">http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css</a>"> <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js</a>"></script> <script src="<a href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" rel="nofollow">http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js</a>"></script> </head> <body> <div class="container" style="margin-top:20px;"> <h3>product</h3> <table class="table table-bordered"> <tr> <th>Id</th> <th>Name</th> <th>Status</th> <th>Price</th> <th>Quantity</th> <th>Sub Total</th> </tr> @foreach (var product in Model) { <tr> <td>@product.Id</td> <td>@product.Name</td> <td>@product.Status</td> <td>@product.Price</td> <td>@product.Quantity</td> <td>@(product.Price * product.Quantity)</td> </tr> } </table> <br /> @Html.PagedListPager(Model,page=>Url.Action("Index",new { page,pageSize=Model.PageSize })) </div> </body> </html> </code></pre> <p> </p> <p>ALINTIDIR </p> <p><a href="http://www.myavuz.net/MakaleDetay/aspnet-mvc/28/aspnet-mvc-pagination-entity-framework-sayfalama---pagelist">http://www.myavuz.net/MakaleDetay/aspnet-mvc/28/aspnet-mvc-pagination-entity-framework-sayfalama---pagelist</a></p>
0 Yorum