按页分类表

时间:2016-06-27 03:07:13

标签: c# html css asp.net razor

我正在VS2012中使用Razor创建一个网页,它会在表格中列出一堆可用的项目。问题是,这个表可以很长。有没有办法按页面分隔这些行(表中每个页面中可能有5个项目)而不使用webgrid或Javascript?

代码:

@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" />
} 

1 个答案:

答案 0 :(得分:0)

这可以通过名为 PagedList.Mvc 的NuGet包实现,完整的分步教程 - here

  1. 在Visual Studio中打开包管理器控制台
  2. 默认项目设置为您的网络应用程序
  3. 在控制台中粘贴Install-Package PagedList.Mvc,然后按Enter
  4. 打开 VIEWS 文件夹中的web.config文件,将下面的命名空间打开到<namespaces>元素

    <add namespace="PagedList"/>
    <add namespace="PagedList.Mvc"/>
    
  5. <强>控制器:

    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};
        }
    }
    

    查看:

    @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 }))
    

    <强>输出:

    MVC paged list