自定义标记助手'分页'
[HtmlTargetElement("div", Attributes = "page-model")]
public class PageLinkTagHelper : TagHelper
{
private IUrlHelperFactory urlHelperFactory;
public PageLinkTagHelper(IUrlHelperFactory helperFactory)
{
urlHelperFactory = helperFactory;
}
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public PagingInfo PageModel { get; set; }
public string PageRoute { get; set; }
[HtmlAttributeName(DictionaryAttributePrefix = "page-url-")]
public Dictionary<string, object> PageUrlValues { get; set; } = new Dictionary<string, object>();
public bool PageClassesEnabled { get; set; } = false;
public string PageClass { get; set; }
public string PageClassNormal { get; set; }
public string PageClassSelected { 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.RouteUrl(PageRoute, new { page = i });
PageUrlValues["page"] = i;
tag.Attributes["href"] = urlHelper.RouteUrl(PageRoute, PageUrlValues);
if (PageClassesEnabled)
{
tag.AddCssClass(PageClass);
tag.AddCssClass(i == PageModel.CurrentPage
? PageClassSelected : PageClassNormal);
}
tag.InnerHtml.Append(i.ToString());
result.InnerHtml.AppendHtml(tag);
}
output.Content.AppendHtml(result.InnerHtml);
}
}
查看
<div page-model="@Model.PagingInfo" page-route="@(Model.IsSysParamCategory ? RouteUrl.Name.SystemListPagination : RouteUrl.Name.CategoryListPagination)" page-classes-enabled="true" page-class="btn mt-2 mb-2" page-class-normal="btn-secondary" page-class-selected="btn-outline-primary" class="btn-group pull-right m-1"></div>
控制器 - 路线
[Route("Admin/System/[action]/{page:int}", Name = RouteUrl.Name.SystemListPagination)]
[Route("Admin/System/[action]/{page:int}", Name = RouteUrl.Name.CategoryListPagination)]
public ViewResult List(int valueId, bool isSysParamCategory, DeleteCategoryType deletedCategory, SaveCategoryType savedCategory, int page = 1)
{ }
错误: InvalidOperationException:找不到视图“列表”。搜索了以下位置: /Areas/Admin/Views/Shared/List.cshtml /Views/Shared/List.cshtml
问题和反思
我只是不明白:)我正在使用这个路径名称:'Admin/System/List/{page:int}'
或'Admin/Category/List/{page:int}'
,具体取决于逻辑标志,但MVC框架是'Shared'
文件夹中的搜索视图。查看位于:Areas/Admin/Views/CategoryType/List