dimanche 3 janvier 2021

"page-class-enabled" and related attributes not working

I learn ASP.NET Core MVC with Adam Freeman's book, but when I practiced example for pagination, the attributes

  1. page-class-enabled="true"
  2. page-class="btn"
  3. page-class-normal="btn-secondary"
  4. page-class-selected="btn-primary"

are not working and actually not found when I type them in the needed tag:

<div page-model="@Model.PagingInfo" page-action="List" page-class-enabled="true"
         page-class="btn" page-class-normal="btn-secondary"
         page-class-selected="btn-primary" class="btn-group pull-right m-1">
</div>

I created the same PagingInfo view model and the PageLinkTagHelper so that the page-model and page-action attributes work correctly (the expected 3 links for 3 pages are shown), but they are just raw html without any styles:

Actual result

I suppose it may be caused because the example is for the 2.0 version, but I use newer 3.1 version. How can I reach the same result now? May be there is another reason? I tried to search but I found almost nothing regarding these attributes, so could you explain what is responsible for them?

Here is code from my _ViewImports file:

@using SportsStore.Models
@using SportsStore.Models.ViewModels
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper SportsStore.Infrastructure.*, SportsStore

The code of the custom tag helper:

namespace SportsStore.Infrastructure
{
    [HtmlTargetElement("div", Attributes = "page-model")]
    public class PageLinkTagHelper : TagHelper
    {
        private IUrlHelperFactory _urlHelperFactory;
        public PageLinkTagHelper(IUrlHelperFactory urlHelperFactory) =>
            _urlHelperFactory = urlHelperFactory;
        [ViewContext]
        [HtmlAttributeNotBound]
        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 = _urlHelperFactory.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 { page = i });
                tag.InnerHtml.Append(i.ToString());
                result.InnerHtml.AppendHtml(tag);
            }
            output.Content.AppendHtml(result.InnerHtml);
        }
    }
}

Code of paging info view model:

namespace SportsStore.Models.ViewModels
{
    public class PagingInfo
    {
        public int TotalItems { get; set; }
        public int ItemsPerPage { get; set; }
        public int CurrentPage { get; set; }
        public int TotalPages => 
            (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);
    }
}



Aucun commentaire:

Enregistrer un commentaire