English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
أولاً، عدم إرسال بيانات
الخطوة الأولى، إنشاء Controller خالي يُسمى PageIndex، وتعريف طريقة كما يلي:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount) { //int count = db.Product.Count(); ViewBag.PageCount = pageCount;//الحصول على عدد الصفحات الكلية من العملية وإدخالها في صفحة عرض التصفية ViewBag.CurrentPage = currentPage;//الحصول على رقم الصفحة الحالية من العملية وإدخالها في صفحة عرض التصفية ViewBag.action = action; ViewBag.controller = controller; return PartialView(); }
تم إدخال أربعة معلمات:
action: العملية (العرض الذي يتم تصفحه،افتراضيًا هو Index);
controller: التحكم؛
currentPage: الصفحة الحالية;
pageCount: عدد الصفحات الكلية للبيانات
الخطوة الثانية:إضافة عرض (PageIndex)
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0) { <span>مرحبًا، لا توجد بيانات للعرض!</span> } else { إذا (ViewBag.CurrentPage <= 10) { <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)"> الصفحة الرئيسية</a>|</span> } else { <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)"> الصفحة الرئيسية</a> <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)"> ...<a> </span> } للدوران (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++) { إذا (i <= 0) { continue; } if (i > ViewBag.PageCount) { break; } <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)"> الصفحة @i</a>|</span> } if (ViewBag.CurrentPage > 1) { <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)"> الصفحة السابقة</a>|</span> } if (ViewBag.PageCount > ViewBag.CurrentPage) { <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)"> الصفحة التالية</a></span> } if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10) { <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)"> الصفحة الأخيرة</a> } else { <span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)"> ...</a></span> <a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)"> الصفحة الأخيرة</a> } <span style="padding-left: 20px">الصفحة الحالية: @ViewBag.CurrentPage | إجمالي @ViewBag.PageCount صفحة</span> </span> }
الخطوة الثالثة:تعديل ملف التحكم في عرض العملية
public ViewResult Index(int? pageIndex) { int pageInd = pageIndex.HasValue ? pageIndex.Value : 1; ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0); //هذا take، عرض 20 في كل صفحة return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20)); }
الخطوة الرابعة:الصفحة الطلبية (أي الخطوة الأخيرة)
@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
عادةً، تكون البيانات متغيرة.
ثانيًا، هناك إرسال بيانات
الخطوة الأولى:إقامة Controller فارغ باسم PageIndex، وتعريف طريقة كما يلي:
public ActionResult PageIndexKey(int currentPage, int pageCount) { ViewBag.PageCount = pageCount;//الحصول على عدد الصفحات الكلية من العملية وإدخالها في صفحة عرض التصفية ViewBag.CurrentPage = currentPage;//الحصول على رقم الصفحة الحالية من العملية وإدخالها في صفحة عرض التصفية return PartialView(); }
الخطوة الثانية:إقامة عرض توزيع
<script> $(function () { $("#pageingByForm a").click(function (event) { $("#pageIndex").val($(this).attr("pageIndex")); //$(this).parent("Form").submit(); document.getElementsByTagName("Form").item(0).submit(); event.preventDefault(); }); }); </script> @Html.Hidden("pageIndex") <div id="pageingByForm"> @if (ViewBag.PageCount == null || ViewBag.PageCount == 0) { <span>لا توجد بيانات حالياً</span> } else { إذا (ViewBag.CurrentPage <= 10) { <span><a pageindex="1" href="#">الصفحة الأولى</a>|</span> } else { <span><a pageindex="1" href="#">الصفحة الأولى</a>|</span> <span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span> } للدوران (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++) { إذا (i <= 0) { continue; } if (i > ViewBag.PageCount) { break; } <span><a pageIndex="@i" href="#">الصفحة @i</a>|</span> } if (ViewBag.CurrentPage >1) { <span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">الصفحة السابقة</a>|</span> } if (ViewBag.PageCount > ViewBag.CurrentPage) { <span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">الصفحة التالية</a></span> } if (ViewBag.CurrentPage >= ViewBag.PageCount - 10) { } else { <span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span> <span><a pageIndex="@ViewBag.PageCount" href="#">الصفحة الأخيرة</a></span> } <span style="padding-left: 20px">الصفحة الحالية: @ViewBag.CurrentPage | إجمالي @ViewBag.PageCount صفحة</span> </span> } </div>
الخطوة الثالثة:رؤية العملية ووحدة التحكم في التحرير
public ViewResult Index(int? pageIndex ,string search) { int pageInd = pageIndex.HasValue ? pageIndex.Value : 1; ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0); return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20)); }
الرؤية (الصفحة المطلوبة):
@using (Html.BeginForm())
{
الحصول على نتائج البحث بناءً على الجنس
الجنس: @Html.TextBox("sex")
<input type="submit" value="البحث" />
@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
}
مثال:
// بيانات، مجموعة من list List<string> s = new List<string>(); s.Add("张军"); ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0); return View(s.Skip((pageInd - 1) * 20).Take(20)); @Html.Action("PageIndex", "PageIndex", new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
هذا هو نهاية محتوى هذا المقال، نأمل أن يكون قد ساعد في تعلمكم، ونرجو منكم أن تدعموا وتشجعوا تعليمات呐喊.
بيان: محتوى هذا المقال تم جمعه من الإنترنت، وله حقوق الملكية للمالك الأصلي، تم جمعه من قبل المستخدمين على الإنترنت وتم تحميله بشكل تلقائي، هذا الموقع لا يمتلك حقوق الملكية، لم يتم تعديل المحتوى بشكل يدوي، ولا يتحمل هذا الموقع أي مسؤولية قانونية متعلقة بذلك. إذا كنت قد وجدت محتوى يشتبه في حقوق النسخ، فنرجو منك إرسال بريد إلكتروني إلى: notice#oldtoolbag.com (الرجاء استبدال # ب @ عند إرسال البريد الإلكتروني) لإبلاغنا، وقدم الأدلة ذات الصلة، إذا تم التحقق من ذلك، فإن هذا الموقع سيقوم بإزالة المحتوى المزعوم بشكل فوري.