我正在阅读Adam Freeman的“ Pro ASP.NET Core MVC 2”书,并关注该书的项目SportsStore。 我在使用标签助手时遇到问题,当我运行项目时,底部的导航链接不起作用,而是在页面源代码中看到了标签助手:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SportsStore V2</title>
</head>
<body>
<div>
<div>
<h3>Kayak</h3>
A boat for one person
<h4>275,00 €</h4>
</div>
<div>
<h3>Lifejacket</h3>
Protective and fashionable
<h4>48,95 €</h4>
</div>
<div>
<h3>Soccer Ball</h3>
FIFA-approved size and weight
<h4>19,50 €</h4>
</div>
<div>
<h3>Corner Flags</h3>
Give your playing field a professional touch
<h4>34,95 €</h4>
</div>
<div page-model="SportsStoreApp.Models.ViewModels.PagingInfo" page-action="List"></div>
</div>
</body>
</html>
List.cshtml的代码
@model ProductListViewModel
@foreach (var p in Model.Products)
{
<div>
<h3>@p.Name</h3>
@p.Description
<h4>@p.Price.ToString("c")</h4>
</div>
}
<div page-model="@Model.PagingInfo" page-action="List"></div>
PageLinkTagHelper类的代码
namespace SportsStoreApp.Infrastructure
{
[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 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 {productPage = i});
tag.InnerHtml.Append(i.ToString());
result.InnerHtml.AppendHtml(tag);
}
output.Content.AppendHtml(result.InnerHtml);
}
}
}
由于我没有ASP.NET或MVC的经验,所以我不知道如何开始解决此问题?
我尝试从头开始重写PS,没有结果。
答案 0 :(得分:0)
我已经跳到有关TagHelpers的章节,并写了实际注册taghelper的意思,这是一个命名问题/错别字/范围错误。
当我在_ViewsImport中注册TagHelper时,我使用了他的书中的程序集名称-SportsStore。但是在我的项目中,我想区分解决方案名称和项目名称,所以我的解决方案名称是SportsSotreV2和项目名称(程序集名称)SportsStoreApp。如此改变
@addTagHelper SportsStoreApp.Infrastructure。*,SportsStore
到
@addTagHelper SportsStoreApp.Infrastructure。*,SportsStoreApp
解决了问题。