1
我如何添加自定義分頁到我的應用程序,它是用C#MVC5在Bootstrap中使用數據庫和實體框架6編寫的?如何在ASP.net MVC 5中進行自定義分頁取決於引導?
我如何添加自定義分頁到我的應用程序,它是用C#MVC5在Bootstrap中使用數據庫和實體框架6編寫的?如何在ASP.net MVC 5中進行自定義分頁取決於引導?
這是我的代碼和它的正常工作:
注:此頁面的代碼需要引導是存在
在模型中添加新的文件名爲paging.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;
namespace project.Models
{
public class Paging
{
private projectEntities db = new projectEntities();
public string Pagination(int total, int page, int Take, int offset, string Controler, string View, string Params)
{
if (total > 0)
{
string c_url = HttpContext.Current.Request.Url.AbsoluteUri.ToLower();
string URL = c_url.Substring(0, c_url.IndexOf(Controler.ToLower()));
double rowPerPage = Take;
if (Convert.ToDouble(total) < Take)
{
rowPerPage = Convert.ToDouble(total);
}
int totalPage = Convert.ToInt16(Math.Ceiling(Convert.ToDouble(total)/rowPerPage));
int current = page;
int record = offset;
int pageStart = Convert.ToInt16(Convert.ToDouble(current) - Convert.ToDouble(offset));
int pageEnd = Convert.ToInt16(Convert.ToDouble(current) + Convert.ToDouble(offset));
string numPage = "";
if (totalPage < 1) return "";
numPage += "<ul class='pagination'>";
if (current > 1) numPage += "<li class='previous'><a href='" + URL + Controler + View + "?Page=" + (page - 1) + Params + "' aria-label='Previous'>«</a></li>";
else numPage += "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
if (current > (offset + 1)) numPage += "<li><a href='" + URL + Controler + View + "?Page=1" + Params + "' name='page1'>1</a></li><li class='disabled spacing-dot'><a href='#'>...</a></li>";
for (int i = 1; i <= totalPage; i++)
{
if (pageStart <= i && pageEnd >= i)
{
if (i == current) numPage += "<li class='active'><a href='#'>" + i + " <span class='sr-only'>(current)</span></a></li>";
else numPage += "<li><a href='" + URL + Controler + View + "?Page=" + i + Params + "'>" + i + "</a></li>";
}
}
if (totalPage > pageEnd)
{
record = offset;
numPage += "<li class='disabled spacing-dot'><a href='#'>...</a></li><li><a href='" + URL + Controler + View + "?Page=" + (totalPage) + Params + "'>" + totalPage + "</a></li>";
}
if (current < totalPage) numPage += "<li class='next'><a class='ui-bar-d' href='" + URL + Controler + View + "?Page=" + (page + 1) + Params + "'>»</a></li>";
else numPage += "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>»</span></a></li>";
numPage += "</ul>";
return numPage;
}
else
{
return "no records found";
}
}
}
}
你可以在您的控制器中使用,如下所示:
Paging pg = new Paging();
private projectEntities db = new projectEntities();
//ex : Controller name-> news , view_name -> browse
public ActionResult Index()
{
int offset = 1;
int Page = 1;
int Take = 20;
if (Convert.ToInt32(Request.QueryString["Page"]) > 1)
{
Page = Convert.ToInt32(Request.QueryString["Page"]);
}
int skip = 0;
if (Page == 1)
skip = 0;
else
skip = ((Page - 1) * Take);
int total = db.Table_Name.Count();
var data = db.Table_Name.Skip(skip).Take(Take);
string paging = pg.Pagination(total, Page, Take, offset, "news", "/browse", "");
ViewBag.Paging = paging;
return View(data.ToList());
}
之後,您可以調用它這樣的觀點:
@model IEnumerable<project.Models.project>
@{
ViewBag.Title = "News";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
<table class="table table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.field_name)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.field_name)
</td>
</tr>
}
</table>
@Html.Raw(ViewBag.Paging)
是一個問題嗎?請使用博客發佈技術文章。 – Prasoon
雖然它被鼓勵在stackoverflow回答你自己的問題,併發布你已經有一個解決方案的問題,你應該寫一個問題描述的實際問題,你的答案答案,以便它可以找到其他人,也可以被別人回答可能是更好的解決方案。不要(基本上)寫「我寫了一些代碼,這是它」。目前,你的實際問題只是一行,並不能作爲堆棧溢出的一個好問題。並從您的答案中刪除「最佳途徑......」,您可能會對解決您的問題的不同方式感到驚訝。 – Solarflare
謝謝,它現在更新 –