我买了专业的asp.net mvc2框架书。我卡在了第 122 页。我不明白为什么它会起作用。
我已经给作者发了邮件,还没有收到任何回复。这是代码,有人可以向我解释它为什么有效。
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");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
这个 PageLinks 辅助方法需要 3 个参数,但在本书后面,当作者调用它时,
<%: Html.PageLinks(
new PagingInfo { CurrentPage = 2, TotalItems = 28, ItemsPerPage = 10 },
i => Url.Action("List", new{ page = i})
) %>
作者只传入了2个参数,缺少HtmlHelper html,但它仍然有效。不明白为什么,求助,谢谢。
最佳答案
扩展方法的第一个参数是调用它的对象。 (Html
在您的示例中)。
关于c# - 请解释为什么这个 c# 扩展方法有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492635/