在ASP.net MVC中一次显示有限的页面链接数

时间:2013-12-24 12:17:03

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

我在ASP.net MVC中借助PagedList显示页面链接。但问题是如果有100页然后所有链接同时显示但我想一次显示10个链接。如何实现。请帮忙。谢谢..以下是工作代码。

Index.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));
        }


    }
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题&amp;通过研究以下代码解决了这个问题: -

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

您必须使用webgrid。

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