Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Naveed Ali 161 posts 426 karma points
    Jun 15, 2016 @ 16:53
    Naveed Ali
    0

    Pagination

    Hi ,

    I am wanting to add pagination to a page on my website. I have got it working using razor..but I would like to do this through a surface controller as the site is going to be content heavy..

    I cannot find no examples of doing this in a controller has anyone got an example???

    Thanks

    Nav

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jun 15, 2016 @ 18:42
    Alex Skrypnyk
    0

    Hi Naveed,

    It's not hard isuue to add pagination, but it will be perfect to use Examine for search node if you have a lot.

    Can you show your code of getting data ?

    Thanks

  • Naveed Ali 161 posts 426 karma points
    Jun 15, 2016 @ 20:25
    Naveed Ali
    0

    Hi alex,

    I have not yet started it in the controller. In my razor view i am just getting the data through ipublishedcontent.

    Do you have any sample code or link to some which shows pagination in surface controller

    Thanks

    Nav

  • Naveed Ali 161 posts 426 karma points
    Jun 16, 2016 @ 09:45
    Naveed Ali
    0

    this is my razor view. So i want to implement this into my controller somehow.

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "pageWithSidebar.cshtml"; 
        var pageSize = 4;
        var page = 1; int.TryParse(Request.QueryString["page"], out page);
        var items = Model.Content.Children().Where(x => x.IsDocumentType("blogDetail")).OrderByDescending(x => x.CreateDate);
        var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);
    
    }
    <h2>@Umbraco.Field("pageTitle")</h2>
    @Umbraco.Field("bodyText")
    @{
        <div class="row">
    
            @if (page > totalPages)
            {
            page = totalPages;
            }
            @if (page < 1)
            {
            page = 1;
            }
    
            @foreach (var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x => x.Name))
            {
    
    
            <div class="col-sm-6 blog-post">
                <div class="col-sm-12">
                    <img src="@Umbraco.Media(@item.GetPropertyValue("blogImage")).Url">
                    <div class="col-sm-12 text-block">
                        <h2>@item.GetPropertyValue("pageTitle")</h2>
                        <ul>
                            <li><span class="heavy">Author:</span> @item.GetPropertyValue("author")</li>
                            <li><span class="heavy">Posted on:</span>@item.CreateDate.ToString("dd MMMM yyyy")</li>
                        </ul>
                        <p class="summary-text">@Umbraco.Truncate(item.GetPropertyValue("bodyText").ToString(), 150)</p>
                    </div>
                </div>
                <a href="@item.Url"><span class="btn">Read this post</span></a>
    
            </div>
    
    
            }
    
        </div>
    
        if (totalPages > 1)
        {
            <nav>
                <ul class="pagination">
                    @if (page > 1)
                    {
                        <li><a href="?page=@(page-1)">Prev</a></li>
                    }
                    @for (int p = 1; p < totalPages + 1; p++)
                    {
                        <li class="@(p == page ? "active" : string.Empty)">
                            <a href="?page=@p">@p</a>
                        </li>
                    }
                    @if (page < totalPages)
                    {
                        <li><a href="?page=@(page+1)">Next</a></li>
                    }
                </ul>
            </nav>       
        }
    }
    
  • Naveed Ali 161 posts 426 karma points
    Jun 16, 2016 @ 10:57
    Naveed Ali
    0

    This is what I have in my controller so far:

      [ChildActionOnly]
            public ActionResult RenderBlogItems()
            {
                var blogPage = Umbraco.TypedContentAtRoot().FirstOrDefault(
                    n => n.DocumentTypeAlias.Equals(UmbracoConstants.DocumentTypeAlias.BlogsPage));
                //var getPage = ;
                var pageSize = 4;
                var page = 1; int.TryParse(Request.QueryString["page"], out page);
                var BlogPages = blogPage.Children().Where(x => x.IsDocumentType("blogDetail")).OrderByDescending(x => x.CreateDate);
                var totalPages = (int)Math.Ceiling((double)BlogPages.Count() / (double)pageSize);
    
            if (page > totalPages)
            {
            page = totalPages;
            }
            else if (page < 1)
            {
            page = 1;
            }
    
            foreach (var blogitems in BlogPages.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x => x.Name))
            {
              var model = new List<BlogsModel>();
              var blog = new BlogsModel()
              {
                  BlogImage = blogitems.GetContentFromPicker(UmbracoConstants.BlogsPage.BlogImage, 0),
                  AuthorImage = blogitems.GetContentFromPicker(UmbracoConstants.BlogsPage.AuthorImage, 0),
    
                  PageTitle = blogitems.GetPropertyValue<string>(UmbracoConstants.BlogsPage.PageTitle),
                  BodyText = blogitems.GetPropertyValue<string>(UmbracoConstants.BlogsPage.BodyText),
                  AuthorName = blogitems.GetPropertyValue<string>(UmbracoConstants.BlogsPage.AuthorName),
                  AuthorDesc = blogitems.GetPropertyValue<string>(UmbracoConstants.BlogsPage.AuthorDesc),
              };
              model.Add(blog);
            }
                return PartialView("");
            }
    

    This is my Model:

      public class BlogsModel
        {
            //public IEnumerable<IPublishedContent> BlogItems { get; set; }
    
            public IPublishedContent BlogImage { get; set; }
            public IPublishedContent AuthorImage { get; set; }
            public string PageTitle { get; set; }
            public string BodyText { get; set; }
            public string AuthorName { get; set; }
            public string AuthorDesc { get; set; }
        }
    
  • Aditya 24 posts 128 karma points
    Nov 05, 2016 @ 06:15
    Aditya
    0

    Thanks for this code - I used it to set up pagination in my blog. I'm doing it in the razor template for now, but may move it to a controller later.

    Is it faster in the controller?

  • Naveed Ali 161 posts 426 karma points
    Nov 05, 2016 @ 12:10
    Naveed Ali
    0

    Yes it is faster. Its best to take approach of putting all logic in a controller.

  • Aditya 24 posts 128 karma points
    Nov 06, 2016 @ 19:48
    Aditya
    0

    Also, what is the Umbraco Constants? Is that part of the API (I can't find it), or something you created to set constants?

    thanks!

  • Aditya 24 posts 128 karma points
    Nov 05, 2016 @ 23:50
    Aditya
    0

    What does your template/view look like?

  • Aditya 24 posts 128 karma points
    Nov 08, 2016 @ 06:15
    Aditya
    0

    Hey,

    I was able to get mine working. Your code was useful, thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies