【发布时间】:2016-08-20 18:40:41
【问题描述】:
我遇到了分页问题。我有一个产品模型,它有一个字符串 ProductCategory 属性。一页可以放4个产品,超过4就指出第2页。问题是当我点击“汽车”类别,点击第2页时,它会带走每一个产品,而不是只取“汽车”。
我从 apress 出版的《ASP.NET MVC 4》一书中得到了这本书。
这是我的 ProductListViewModel:
public class ProductsListViewModel
{
public List<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
}
这是我的ProductController的List Action:当我调试应用程序时,在第2页的点击处,类别参数为空。
public ViewResult List(string category, int page = 1)
{
ProductsListViewModel model = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.ProductCategory.Equals(category))
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ?
repository.Products.Count() :
repository.Products.Where(e => e.ProductCategory == category).Count()
}
};
model.CurrentCategory = category;
return View(model);
}
这是我的列表视图:
@model SportsStore.WebUI.Models.ProductsListViewModel
@{
ViewBag.Title = "Products";
}
@foreach (var p in Model.Products)
{
<div class="item">
@Html.Partial("ProductSummary", p)
</div>
}
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, ProductCategory = Model.CurrentCategory }))
ProductSummary 是查看产品的局部视图。 Pagelinks 是一种扩展方法:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo,
Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.Append(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
如上图所示,当我点击第 2 页时,它会获取所有产品,而不是汽车。我该如何解决? 提前致谢。
注意:以下已添加路线:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,
"",
new
{
controller = "Product",
action = "List",
category = (string)null,
page = 1
}
);
/*
*http://localhost:56701/?page=2 olmasındansa http://localhost:56701/Page4 olmasını sağlayan kod parçacığı.
*
*/
routes.MapRoute(null,
"Page{page}",
new { controller = "Product", action = "List", category = (string)null },
new { page = @"\d+" }
);
routes.MapRoute(null,
"{category}",
new { controller = "Product", action = "List", page = 1 }
);
routes.MapRoute(null,
"{category}/Page{page}",
new { controller = "Product", action = "List" },
new { page = @"\d+" }
);
routes.MapRoute(null, "{controller}/{action}");
}
}
正如您在下面看到的,在第 2 页,每个汽车项目都存在。 (蓝色矩形)。但我不想在页面底部看到第 3 页和第 4 页(红色矩形)。 提前致谢。
【问题讨论】:
-
您能否提供您设置的路线,查看您提供的 cod,这可能是最有可能开始寻找问题的地方
-
亲爱的 Paul Hadfield,我已经更新了帖子。
-
当您点击汽车产品的第二页时,您能否添加一个显示 URL 的屏幕截图。您还可以确认,当您单击类别时,它会显示正确的列表,只有在查看类别并单击查看页面 2+ 时才会出错
-
是的,我确认。我不在我的电脑,我只能明天发布截图。
-
当我点击汽车时,我的电脑上出现了localhost:56701/Car,但是所有页面都显示出来了。 (我希望页面底部有第1页和第2页,但是,第3页和第4页也存在。)当我点击汽车类别的第2页时,地址栏出现localhost:56701/Car/Page2,只有汽车出现在page,但是,page 3 和 page 4 也存在于底部。