【问题标题】:Show Limited Number of Page Links at a time in ASP.net MVC在 ASP.net MVC 中一次显示有限数量的页面链接
【发布时间】:2013-12-24 12:17:02
【问题描述】:

我在 ASP.net MVC 中借助 PagedList 显示页面链接。但问题是如果有 100 个页面,那么所有链接都会同时显示,但我想一次显示 10 个链接。怎么可能实现。请帮忙。谢谢.. 下面是工作代码。

索引.cshtml

@model PagedList.IPagedList<MvcPagingList.Models.Employee>
@{
    ViewBag.Title = "Employees";
}
<link href="@Url.Content("~/bootstrap/css/bootstrap.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/bootstrap/css/bootstrap.css")" rel="stylesheet" type="text/css" />
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
    <p>
        Search By Name : @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        &nbsp;
        <input type="submit" value="Search" /></p>
}
<table>
    <tr>
        <th>
            @Html.Label("Last Name")
        </th>
        <th>
            @Html.Label("First Name")
        </th>
        <th>
            @Html.Label("Department ID")
        </th>
        <th>
            @Html.ActionLink("Salery", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DepartmentID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.EmployeeID }) |
                @Html.ActionLink("Details", "Details", new { id = item.EmployeeID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.EmployeeID })
            </td>
        </tr>
    }
</table>

<div class="pagination pagination-right">
    <ul>
    <li> 
        @for (int p = 1; p <= Model.PageCount; p++)
        {
            <a  href="@Url.Action("Index", "Home", new { page = p })">@p</a>
        }
        </li>
        </ul>
    </div>

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPagingList.Models;
using PagedList;

namespace MvcPagingList.Controllers
{
    public class HomeController : Controller
    {

        EmployeeEntities dbEntities = new EmployeeEntities();

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Salery desc" : "";
            //ViewBag.DateSortParm = sortOrder == "HireDate" ? "HireDate desc" : "HireDate";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var employees = from s in dbEntities.Employees
                            select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                employees = employees.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
                                       || s.FirstName.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Salery desc":
                    employees = employees.OrderByDescending(s => s.LastName);
                    break;
               default:
                    employees = employees.OrderBy(s => s.LastName);
                    break;
            }

            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(employees.ToPagedList(pageNumber, pageSize));
        }


    }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    我遇到了同样的问题并通过研究以下代码解决了它:-

    http://msdn.microsoft.com/en-us/magazine/gg650669.aspx

    您必须为此使用 webgrid。

    http://www.w3schools.com/aspnet/showfile_c.asp?filename=try_webpages_cs_004

    【讨论】:

    • 我在页面上显示它。即一次 10 条记录,但我想一次显示有限数量的页面链接...感谢您的上述链接
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2011-04-08
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多