Section header not displaying correctly in ASP.NET Core view

huangapple go评论74阅读模式
英文:

Section header not displaying correctly in ASP.NET Core view

问题

I am migrating an application from ASP.NET MVC 5 running on .NET 4.8 to ASP.NET Core 6 MVC.

The MVC 5 application has the following in an Index.cshtml file:

@section header
{
    @Html.Partial("~/Views/Shared/DisplayTemplates/_ModeHeader.cshtml", Model.Mode, new ViewDataDictionary { { "text", "Salaries" }, { "icon", "fa-money-check" } })
}

I updated it to the following in the new application:

@section header {
    @await Html.PartialAsync("~/Views/Shared/DisplayTemplates/_ModeHeader.cshtml", Model.Mode, new ViewDataDictionary(ViewData) { { "text", "Salaries" }, { "icon", "fa-money-check" } })
}

_ModeHeader.cshtml:

@using Project.Extensions.MVC
@using Project.Models

@model Enums.Mode

@{
    if (!ViewData.ContainsKey("icon"))
    {
        ViewData["icon"] = "fa-list-alt";
    }
}

<div class="border-bottom row">
    <div class="col">
        <span class="h1">
            @switch (Model)
            {
                case Enums.Mode.None:
                    break;
                case Enums.Mode.Browse:
                    @IconExtensions.BuildColoredIconTag(ViewData["icon"].ToString(), "text-info")
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            @ViewData["text"]
        </span>
    </div>
</div>

The expected result is an icon, a space, and the text Salaries.

The current result is the text Normal, a space, and the text Salaries.

UPDATE:

The text "Normal" is the result of updating the line var finalHtml = icon.ToString(TagRenderMode.Normal); from the old application to var finalHtml = icon.TagRenderMode.ToString();. Since TagRenderMode is "Normal" by default, that is where the value comes from.

IconExtensions.cs:

public static class IconExtensions
{
    public static HtmlString BuildIconTag(string iconName)
    {
        var icon = BuildInternalIconTag(iconName);

        var htmlBuilder = new StringBuilder();

        htmlBuilder.Append(icon);

        var finalHtml = icon.TagRenderMode.ToString();

        return new HtmlString(finalHtml);
    }

    public static HtmlString BuildColoredIconTag(string iconName, string color)
    {
        var icon = BuildInternalColoredIconTag(iconName, color);

        var htmlBuilder = new StringBuilder();

        htmlBuilder.Append(icon);

        var finalHtml = icon.TagRenderMode.ToString();

        return new HtmlString(finalHtml);
    }

    public static TagBuilder BuildInternalIconTag(string iconName)
    {
        var icon = new TagBuilder("i");

        icon.AddCssClass("fas");
        icon.AddCssClass("fa-fw");
        icon.AddCssClass(iconName);
        icon.MergeAttribute("aria-hidden", "true");

        return icon;
    }

    public static TagBuilder BuildInternalColoredIconTag(string iconName, string color)
    {
        var icon = new TagBuilder("i");

        icon.AddCssClass("fas");
        icon.AddCssClass("fa-fw");
        icon.AddCssClass(iconName);
        icon.AddCssClass(color);
        icon.MergeAttribute("aria-hidden", "true");

        return icon;
    }
}
英文:

I am migrating an application from ASP.NET MVC 5 running on .NET 4.8 to ASP.NET Core 6 MVC.

The MVC 5 application has the following in an Index.cshtml file:

@section header
{
    @Html.Partial(&quot;~/Views/Shared/DisplayTemplates/_ModeHeader.cshtml&quot;, Model.Mode, new ViewDataDictionary { { &quot;text&quot;, &quot;Salaries&quot; }, { &quot;icon&quot;, &quot;fa-money-check&quot; } })
}

I updated it to the following in the new application:

@section header {
    @await Html.PartialAsync(&quot;~/Views/Shared/DisplayTemplates/_ModeHeader.cshtml&quot;, Model.Mode, new ViewDataDictionary(ViewData) { { &quot;text&quot;, &quot;Salaries&quot; }, { &quot;icon&quot;, &quot;fa-money-check&quot; } })
}

_ModeHeader.cshtml:

@using Project.Extensions.MVC
@using Project.Models

@model Enums.Mode

@{
    if (!ViewData.ContainsKey(&quot;icon&quot;))
    {
        ViewData[&quot;icon&quot;] = &quot;fa-list-alt&quot;;
    }
}

&lt;div class=&quot;border-bottom row&quot;&gt;
    &lt;div class=&quot;col&quot;&gt;
        &lt;span class=&quot;h1&quot;&gt;
            @switch (Model)
            {
                case Enums.Mode.None:
                    break;
                case Enums.Mode.Browse:
                    @IconExtensions.BuildColoredIconTag(ViewData[&quot;icon&quot;].ToString(), &quot;text-info&quot;)
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            @ViewData[&quot;text&quot;]
        &lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;

The expected result is an icon, a space, and the text Salaries.

The current result is the text Normal, a space, and the text Salaries.

UPDATE:

The text "Normal" is the result of updating the line var finalHtml = icon.ToString(TagRenderMode.Normal); from the old application to var finalHtml = icon.TagRenderMode.ToString();. Since TagRenderMode is "Normal" by default, that is where the value comes from.

IconExtensions.cs:

public static class IconExtensions
{
	public static HtmlString BuildIconTag(string iconName)
	{
		var icon = BuildInternalIconTag(iconName);

		var htmlBuilder = new StringBuilder();

		htmlBuilder.Append(icon);

		var finalHtml = icon.TagRenderMode.ToString();

		return new HtmlString(finalHtml);
	}

	public static HtmlString BuildColoredIconTag(string iconName, string color)
	{
		var icon = BuildInternalColoredIconTag(iconName, color);

		var htmlBuilder = new StringBuilder();

		htmlBuilder.Append(icon);

		var finalHtml = icon.TagRenderMode.ToString();

		return new HtmlString(finalHtml);
	}

	public static TagBuilder BuildInternalIconTag(string iconName)
	{
		var icon = new TagBuilder(&quot;i&quot;);

		icon.AddCssClass(&quot;fas&quot;);
		icon.AddCssClass(&quot;fa-fw&quot;);
		icon.AddCssClass(iconName);
		icon.MergeAttribute(&quot;aria-hidden&quot;, &quot;true&quot;);

		return icon;
	}

	public static TagBuilder BuildInternalColoredIconTag(string iconName, string color)
	{
		var icon = new TagBuilder(&quot;i&quot;);

		icon.AddCssClass(&quot;fas&quot;);
		icon.AddCssClass(&quot;fa-fw&quot;);
		icon.AddCssClass(iconName);
		icon.AddCssClass(color);
		icon.MergeAttribute(&quot;aria-hidden&quot;, &quot;true&quot;);

		return icon;
	}
}

答案1

得分: 1

This line is just setting finalHtml to "Normal"

这行只是将finalHtml设置为"Normal"

This line is using just "Normal" to make the HtmlString

这行只是使用"Normal"来创建HtmlString

You are not using your icon object at all at this point.

在这一点上,您根本没有使用icon对象。

You probably want something more like:

您可能希望更像这样:

public static HtmlString BuildColoredIconTag(string iconName, string color)
{
var icon = BuildInternalColoredIconTag(iconName, color);
var writer = new StringWriter();
icon.WriteTo(writer, HtmlEncoder.Default);

return new HtmlString(writer.ToString());

}

英文:

This line is just setting finalHtml to "Normal"

var finalHtml = icon.TagRenderMode.ToString();

This line is using just "Normal" to make the HtmlString

return new HtmlString(finalHtml);

You are not using your icon object at all at this point.

You probably want something more like:

public static HtmlString BuildColoredIconTag(string iconName, string color)
{
    var icon = BuildInternalColoredIconTag(iconName, color);
    var writer = new StringWriter();
    icon.WriteTo(writer, HtmlEncoder.Default);

    return new HtmlString(writer.ToString());
}

huangapple
  • 本文由 发表于 2023年4月11日 01:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定