asp.net-mvc-3viewmodeldata-paging

How to implement paging with Linq to Xml?


I am stuck on how to implment paging when retrieving images from my rss feed. I have a helper also to render the html for the pager links. However i am not sure what to do in the Index action. My code is as follows:

In my controller:

Public Function Index() As ActionResult
        Dim feedurl As String = "http://riderdesign.net/p290530166/recent.rss"
        Using x = XmlReader.Create(feedurl)
            Dim r = XDocument.Load(x)
            Dim mediapfx As XNamespace = "http://search.yahoo.com/mrss/"
            '  Dim rssfeed = SyndicationFeed.Load(x)
            Dim ml = From item In r.Descendants(mediapfx + "content") Select item
            Dim PageSize As Integer = Configuration.ConfigurationManager.AppSettings.Get("PageSize")

            Dim medialist = From item In r.Descendants("item") Select New MediaImage With {
             .Alt = item.Element("title"), .ImageUrl = item.Element("link"), .ThumbnailUrl = TryGetAttributeValue(item.Element(mediapfx + "thumbnail"), "url"), .Height = TryGetAttributeValue(item.Element(mediapfx + "thumbnail"), "height"), .Width = TryGetAttributeValue(item.Element(mediapfx + "thumbnail"), "width")} Take PageSize
            Return View(medialist)

        End Using

    End Function

PagedData class:

    Public Class PagedData
    Public Property TotalItems As Integer

    Public Property ItemsPerPage As Integer

    Public Property CurrentPage As Integer

    ReadOnly Property TotalPages As Integer
        Get
            Return Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(TotalItems / ItemsPerPage)))
        End Get
    End Property

End Class

And my paging view model

 mvcPublic Class PagingViewModel
    Property Images As IEnumerable(Of MediaImage)
    Property Paging As Pager

End Class

Paging helper:

   Public Class PagingHelper
    Public Shared Function PageLinks(ByVal html As HtmlHelper, ByVal Page As Pager, ByVal pageUrl As Func(Of Integer, String)) As MvcHtmlString
        Dim r As StringBuilder = New StringBuilder
        For i = 1 To Page.TotalPages
            Dim tag As TagBuilder = New TagBuilder("a")
            tag.MergeAttribute("href", pageUrl(i))
            tag.InnerHtml = i.ToString
            If i = Page.CurrentPage Then
                tag.AddCssClass("selected")
                r.Append(tag.ToString)

            End If
            Return MvcHtmlString.Create(r.ToString)
        Next

    End Function

Solution

  • I implemented paging using Troy Goode's PagedList and its associated helper. I call the ToPagedList extension method and use the helper to render the links for paging. I did download the souce for the helper and modify it however to render spans instead of an unordered list for the paging links.