I've got this class:
public override void Process(TagHelperContext context,
TagHelperOutput output)
{
if (ViewContext != null && PageModel != null)
{
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);
}
However, the 'tag', 'i' and 'result' attributes after the for-loop don't fall in the scope, which shouldn't be the matter if I'm correct. I'm using VS2022 with SDK version 6.0.402.
CodePudding user response:
TagBuilder result = new TagBuilder("div");
should go outside the for loop:
public override void Process(TagHelperContext context,
TagHelperOutput output)
{
TagBuilder result = new TagBuilder("div");
if (ViewContext != null && PageModel != null)
{
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
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);
}
CodePudding user response:
I'm not sure what you want in the end, but one thing you should pay attention to is that variables defined in a for
loop can only be used in a for
loop, and variables defined in an if
can only be used in an if
.
So tag.InnerHtml.Append(i.ToString());
can only be used in a for loop, unless you define an i
outside the for loop. Like this:
int k = 0;
for (int i = 1; i <= PageModel.TotalPages; i )
{
k=i;//The final value of k is PageModel.TotalPages.Count
}
tag.InnerHtml.Append(k.ToString());
If you want to add all i
in TagBuilder tag
, then it should be like this:
public override void Process(TagHelperContext context,
TagHelperOutput output)
{
TagBuilder result = new TagBuilder("div");
if (ViewContext != null && PageModel != null)
{
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
TagBuilder tag = new TagBuilder("a");
for (int i = 1; i <= PageModel.TotalPages; i )
{
tag.Attributes["href"] = urlHelper.Action(PageAction,
new { productPage = i });
tag.InnerHtml.Append(i.ToString());
}
result.InnerHtml.AppendHtml(tag);
}
output.Content.AppendHtml(result.InnerHtml);
}
If you only want to add the last entry of PageModel.TotalPages
in the TagBuilder tag
, it should be like this:
public override void Process(TagHelperContext context,
TagHelperOutput output)
{
TagBuilder result = new TagBuilder("div");
if (ViewContext != null && PageModel != null)
{
IUrlHelper urlHelper = UrlHelperFactory.GetUrlHelper(ViewContext);
TagBuilder tag = new TagBuilder("a");
for (int i = 1; i <= PageModel.TotalPages; i )
{
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);
}
Hope this can help you.