如果它显示没有可用数据,则问题是db.Products.ToList().AsPagination(page ?? 1, 10) 只是不返回任何元素(空集合)。至于为什么会发生这种情况,从您提供的信息中无法判断。这在很大程度上取决于此ProductDataContext 的实现以及数据存储中的可用数据。
话虽如此,我建议您使用强类型视图:
public ActionResult List(int? page)
{
using (ProductDataContext db = new ProductDataContext())
{
var products = db.Products.ToList().AsPagination(page ?? 1, 10);
return View("product", products);
}
}
所以你的观点就变成了:
<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.Product>>" %>
<%@ Import Namespace="AppName.Models" %>
<%= Html.Grid<Product>(Model)
.Columns(column =>
{
column.For(c => c.CategoryID);
column.For(c => c.SupplierID);
})
%>
注意视图是如何强类型化到产品集合的。
简单、简单、强类型。
更新:
根据 cmets 部分的要求,这里是向每一行添加 Edit 和 Delete 链接的示例:
<%= Html.Grid<Product>(Model)
.Columns(column =>
{
column.For("TableLinks").Named("");
column.For(c => c.CategoryID);
column.For(c => c.SupplierID);
})
%>
在TableLinks.ascx部分:
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Product>" %>
<%@ Import Namespace="AppName.Models" %>
<td>
<%: Html.ActionLink<ProductsController>(c => c.Edit(Model.Id), "Edit") %> |
<% using (Html.BeginForm<ProductsController>(c => c.Destroy(Model.Id))) { %>
<%: Html.HttpMethodOverride(HttpVerbs.Delete) %>
<input type="submit" value="Delete" />
<% } %>
</td>
这当然假设您的 ProductsController 中存在以下操作:
public ActionResult Edit(int id)
...
[HttpDelete]
public ActionResult Destroy(int id)
我还邀请您查看我写的 sample MVC application,它说明了这些概念。