ASP.NET MVC 4에서 샘플을 테스트하고 있습니다. HtmlHelper에 대한 확장 메소드를 만듭니다.이 람다에 전달 된 값
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);
}
에 기초이 뷰에 대한 동작 방법을 정의하는 제어기가
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))
:
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());
}
}
이는 아래 도면에 의해 사용된다 : 아래의 코드는 확장 메소드의 결과 (중단 점 사용), x의 값은 컨트롤러의 List 조치 메소드에서 'page'매개 변수입니다. x가 어떻게 그것을 파악 했습니까? 또는 값이 x에 어떻게 전달 되었습니까? 나는 주로 컬렉션을 사용하여 작업 할 때 LINQ에서 대리인을 주로 사용했습니다. 매개 변수로 전달 된 값은 컬렉션에서 가져온 값입니다 (반복됨). 그러나 나는 이것을 이해할 수없는 것 같습니다.
와우, 라인 태그를 보지 못했다고 생각하지 않습니다 .MergeAttribute ("href", pageUrl (i)). 감사! –