【发布时间】:2014-07-14 15:19:58
【问题描述】:
我正在尝试 ASP.NET MVC 4 中的示例。他为 HtmlHelper 创建了一个扩展方法。下面是代码:
public static class PagingHelpers
{
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());
}
}
这被以下视图使用:
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))
这是定义视图操作方法的控制器:
public ViewResult List(string category, int page = 1)
{
ProductsListViewModel model = new ProductsListViewModel {
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.Products.Count()
},
CurrentCategory = category
};
return View(model);
}
根据扩展方法(并使用断点)的结果,x 的值是控制器中 List 操作方法上的“页面”参数。 x 是如何得到它的?或者值是如何传递给 x 的?在处理集合时,我主要在 LINQ 上使用委托。作为参数传递的值来自集合(迭代)。但我似乎无法理解这一点。
【问题讨论】:
标签: asp.net-mvc lambda