我正在VS2012中使用Razor创建一个网页,它将在表中列出一堆可用项目。问题是,此表可能会很长。有没有一种方法可以不使用webgrid或Javascript按页将这些行分隔开(表中每个页可能有5个项目)?
码:
@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" />
}
最佳答案
这可以通过一个名为PagedList.Mvc的NuGet包来完成,该包逐步完成了教程-here
在Visual Studio中打开程序包管理器控制台
将默认项目:设置为您的Web应用程序
在控制台中粘贴Install-Package PagedList.Mvc
并按Enter
在VIEWS文件夹中打开web.config
文件,并在<namespaces>
元素下面打开命名空间
<add namespace="PagedList"/>
<add namespace="PagedList.Mvc"/>
控制器:
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 }))
输出:
关于c# - 细目表(按页),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38045326/