I am trying to divide listed items into pages by special tags that must be established by custom TagHelper

I have a class to hold data for page and items that will be processed

    namespace SportWeb.Models.ViewModels
    public class PagingInfo
        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); } }

I am wraping it inside an other modelviewdata

namespace SportWeb.Models.ViewModels
    public class ProductListViewModel
        public IEnumerable<Product> Products { get; set; }
        public PagingInfo PagingInfos { get; set; }

Then insert it into Controller Class to retrieve data and establishing logic

    public class ProductController : Controller
        private IProductRepository _iProductRepository;
        int PageSize = 4;
        public ProductController(IProductRepository iProductRepository)
            _iProductRepository = iProductRepository;
        public IActionResult List(int itemPage = 1) => View(new ProductListViewModel 
                { Products = _iProductRepository
                   .List.OrderBy(p => p.ProductID)
                   .Skip((itemPage - 1) * PageSize)

                   PagingInfos = new PagingInfo { 
                     CurrentPage = itemPage, 
                     ItemsPerPage = PageSize, 
                     TotalItems= _iProductRepository.List.Count()} });


And creating my TagHelper class

namespace SportWeb.InfraSturcture
    [HtmlTargetElement("div", Attributes = "page-model")]
    public class PageLinkTagHelper :TagHelper
        private IUrlHelperFactory _iUrlHelperFactory;
        public PageLinkTagHelper(IUrlHelperFactory iUrlHelperFactory)
            _iUrlHelperFactory = iUrlHelperFactory;
        public ViewContext ViewContext { get; set; }
        public PagingInfo PageModel { get; set; }
        public string PageAction { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
            IUrlHelper urlHelper = _iUrlHelperFactory.GetUrlHelper(ViewContext);
            TagBuilder result = new TagBuilder("div");
            for (int i=1; i<PageModel.TotalPages; i++)
                TagBuilder tag = new TagBuilder("a");
                tag.Attributes["href"] = urlHelper.Action(PageAction, new { itempPage = i });

and here is View page codes

    ViewData["Title"] = "List";
    Layout = "~/Views/Shared/_Layout.cshtml";
@model ProductListViewModel
@addTagHelper SportWeb.InfraStructure.*,SportStore


@foreach (var p in Model.Products)
<div page-model="@Model.PagingInfos" page-action="List"></div>

ViewImport codes below

@using SportWeb.Models
@using SportWeb.Models.ViewModels
@using SportWeb.Entity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper SportWeb.InfraStructure.*, SportWeb

But when program runs, navigation panel is not appearing on the page Here navigation panel is not appearing

And When I open page source it seems Tag helper does not work , created tags are not added by the codes.

source page

I do not understand why my tag helper does not work at all. Do you have any idea about where I am making mistake ?

Edit : I am working with CORE 3.0 features. Can it be caused that problem ?

1 Answers

Prolog On

I tried to reproduce your scenario and it worked. I tried with NET Core 2.2 and Visual Studio 2017 15.9.11 and with .NET Core 3.0 Preview 5 with Visual Studio 2019 16.0.3.

Most likely the problem lies on your side. Try to troubleshoot. Start with checking if the tag helper is executed at all. Place a breakpoint in Process() method in your PageLinkTagHelper. See if it is being hit while running the application.

Double check if you are adding the tag helper properly. Properly added tag helper will have Visual Studio IntelliSense, like this:

enter image description here