简体   繁体   English

细目表(按页)

[英]Breakdown table by pages

I'm creating a webpage using Razor in VS2012, where it will list a bunch of available projects in a table. 我正在VS2012中使用Razor创建一个网页,它将在表中列出一堆可用项目。 The problem is, this table can get pretty long. 问题是,此表可能会很长。 Is there a way to separate these rows by pages (maybe 5 projects in each page in the table) without using webgrid or Javascript? 有没有办法在不使用webgrid或Javascript的情况下按页面将这些行分隔开(表中每个页面可能有5个项目)?

Code: 码:

@if (!Request.QueryString["UserID"].IsEmpty()){
    <table>      
    <tr>
    <th >Group ID</th>
    <th >PPMID</th>
    <th >EPRID</th>
    <th >Project Name</th>
    <th >Delete</th>
    </tr>

    @foreach (var row in db.Query(sql_c)) {
    counter++;
    <tr class="@temp">

        <td><br />
            @row.UserID
        </td>

        <td><br />
            @row.PPMID
        </td>

        <td><br />
            @row.EPRID
        </td>

        <td ><br />
            @row.Proj_Name
        </td>

        <td><br /><button type="submit" onclick="return confirm('Are you sure you want to delete?')" id="delete" name="delete" value="@row.Link"/>Delete</td>

    </tr>            
    }
    </table>
    <br /><input style="float:right;font-weight:bold" type="submit" id="add" name="add" value="Add New Project" />
} 

This can be achieved with a NuGet package called PagedList.Mvc , complete step by step tutorial - here 这可以通过一个名为PagedList.Mvc的NuGet包来完成,该包包含分步教程- 这里

  1. Open package manager console in Visual Studio 在Visual Studio中打开程序包管理器控制台
  2. Set the Default project: to your web application 默认项目:设置为您的Web应用程序
  3. Paste Install-Package PagedList.Mvc in the console and press Enter 在控制台中粘贴Install-Package PagedList.Mvc并按Enter
  4. Open web.config file in the VIEWS folder and the namespaces below to the <namespaces> element 打开VIEWS文件夹中的web.config文件,并在<namespaces>元素下面打开<namespaces>

     <add namespace="PagedList"/> <add namespace="PagedList.Mvc"/> 

Controller: 控制器:

public class PagingController : Controller
{
    public ActionResult Index(int?page)
    {
        int pageSize = 3;
        int pageNumber = (page ?? 1);

        List<Item> items = this.GetItems();
        return View(items.ToPagedList(pageNumber, pageSize));
    }

    private List<Item> GetItems()
    {
        var item1 = new Item { Description = "Item 1", Number = "1" };
        var item2 = new Item { Description = "Item 2", Number = "2" };
        var item3 = new Item { Description = "Item 3", Number = "3" };

        var item4 = new Item { Description = "Item 4", Number = "4" };
        var item5 = new Item { Description = "Item 5", Number = "5" };
        var item6 = new Item { Description = "Item 6", Number = "6" };

        var item7 = new Item { Description = "Item 7", Number = "7" };
        var item8 = new Item { Description = "Item 8", Number = "8" };

        return new List<Item> { item1, item2, item3, item4, item5, item6 ,item7, item8};
    }
}

View: 视图:

@model PagedList.IPagedList<MVC_jqgrid_example.Controllers.Item>

@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    Layout = null;
}

<table class="table">
    <tr>
        <th>
            Number
        </th>
        <th>
            Description
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @item.Number
        </td>
        <td>
            @item.Description
        </td>
    </tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Output: 输出:

MVC页面列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM