Cześć, zacząłem uczyć się od pewnego momentu ASP.NET MVC 5 z książki A. Freeeman'a Zaawansowane programowanie. Tylko problem polega na tym, że utknąłem w pewnym momencie. Tutaj wrzucę klasy o które mi chodzi a później napiszę w czym problem
Mam taki model:
public class PaginingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
}
public ViewResult List(string category,int page = 1)
{
ProductListViewModel model = new ProductListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PaginingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
// TotalItems = repository.Products.Count()
TotalItems = category == null ?
repository.Products.Count() : repository.Products.Where(x => x.Category == category).Count()
},
CurrentCategory = category
};
return View(model);
}
oraz
public static MvcHtmlString PageLinks(this HtmlHelper html,PaginingInfo paginingInfo,Func<int,string> pageUrl)
{
StringBuilder result = new StringBuilder(); //sklejka stringu a + href + szczegoly
for(int i=1;i<=paginingInfo.TotalPages;i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));//tera dziala paginacja
tag.InnerHtml = i.ToString(); //wkleja do buttona nr strony
if(i==paginingInfo.CurrentPage)
{
tag.AddCssClass("selected");
tag.AddCssClass("btn-primary");
}
tag.AddCssClass("btn btn-default");
result.Append(tag.ToString()); //kopia do result
}
return MvcHtmlString.Create(result.ToString());
}
oraz wywołanie w widoku (pliku cshtml)
@Html.PageLinks(Model.PagingInfo, page => Url.Action("List", new { page }))
Problem polega u mnie w zrozumieniu PageLinks. Nie mogę zrozumieć po co jako parametr jest Func<int,string> (wiem jak działa, pobiera zmienną typu int i zwraca zmienną typu string), oraz nie wiem jak działa to wywołanie w widoku(po co jest Url.Action, wiem, że on zwraca stringa z adresem url). Ciężko znaleźć jakieś wytłumaczenie tego na internecie, dlatego postanowiłem tutaj napisać. Może ktoś mnie nakierować jak to rozumieć? Jak to działa?