HTML Helper for page link creation - trying to understand
===

Below is the complete source for my HTML helper file (PageHelper.cs) and it works perfectly.  I am learning ASP.NET MVC 3 and using the 'Pro ASP.NET MVC 3 Framework' book by aPress.  I love the flow of the book and am learning a lot, however every now and then it provides working code without a real explanation as to why it works and this is one of these examples.  I've spent a considerable amount of time with Google and Stack on this one. To get to the point now...

I am hoping someone can explain the flow of what is happening within the 'public static MvcHtmlString PageLinks'.  I'm trying to learn this and not simply follow along in the book (NOTE:  The excessive comments in the code are my own - meant to reinforce learning).  

My take on it is <b>MvcHtmlString</b> is used to tell the browser not to reencode the HTML again because the resulting HTML already is.  <b>this</b> is used to capture the page the user is currently on.  <b>html</b> is the instantiation of the <b>HtmlHelper</b> class? (although <b>html</b> is never mentioned again - why is that?).  <b>pagingInfo</b> is the instantiation of my <b>PagingInfo</b> class that holds properties used in the HTML creation that gets returned.  Here is the part I can't wrap my head around at all....the <b>Func</b> part.  The book explains that the <b>Func</b> parameters provide the ability to pass in a delagate that will be used to generate the links to view other pages - not sure what this means and why the function route is needed at all.

The rest of the code I can follow along.  Sorry for the long winded post, but I'm seeking clarity.  If any of my code comments or explanations are incorrect, please correct me.  Thanks in advance!

    using System;
    using System.Text;
    using System.Web.Mvc;
    using SportsStore.WebUI.Models;

    //You use HTML helpers in a view to render HTML content. An HTML helper, in most 
    //cases, is just a method that returns a string.  You can build an entire ASP.NET
    //MVC application without using a single HTML helper. However, HTML helpers make 
    //your life as a developer easier. By taking advantage of helpers, you can build
    //your views with far less work.  Write once, reuse often.  

    //We use 'MvcHtmlString' so that the result doesn't get re-encoded in the view. 
    //It is part of the MVC framework and when you create your own HTML helper
    //methods like this one, always use it.

    namespace SportsStore.WebUI.HtmlHelpers
    {
        //This is public so it can be accessed in other areas, however the 'static'
        //means it can't be instantiated.
        public static class PagingHelpers
        {
            //This is an HTML Helper method that we call 'PageLinks', which generates
            //HTML for a set of page links using the info provided in a PagingInfo
            //object.  Remember that extension methods are only available for use
            //when the namespace that contains it is in scope.  In a code file, this
            //is done with a 'using' statement, but for a Razor view, we must add a 
            //configuration entry to the View-specific Web.config file OR add a
            //'@using' statement to the view itself.  For this project we chose to 
            //use the Web.config file to keep the View less cluttered.

            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());
            }
        }
    }