TagBuilder.MergeAttribute对参数null的问题

时间:2014-06-04 15:22:05

标签: c# asp.net-mvc razor

我想使用Razor的功能,以便在属性的值为null时不在标签内生成属性输出。所以当Razor遇到< div class =" @ var">其中@var为null,输出仅为< div>。我已经创建了一些Html扩展方法来在tag中写入文本。该方法将标题文本,级别(h1..h6)和html属性作为简单对象。代码是:

public static MvcHtmlString WriteHeader(this HtmlHelper html, string s, int? hLevel = 1, object htmlAttributes = null)
{
    if ((hLevel == null) || 
        (hLevel < 1 || hLevel > 4) ||
        (s.IsNullOrWhiteSpace()))
        return new MvcHtmlString("");

    string cssClass = null, cssId = null, cssStyle = null;

    if (htmlAttributes != null)
    {
        var T = htmlAttributes.GetType();

        var propInfo = T.GetProperty("class");
        var o = propInfo.GetValue(htmlAttributes);
        cssClass = o.ToString().IsNullOrWhiteSpace() ? null : o.ToString();

        propInfo = T.GetProperty("id");
        o = propInfo.GetValue(htmlAttributes);
        cssId = o.ToString().IsNullOrWhiteSpace() ? null : o.ToString();

        propInfo = T.GetProperty("style");
        o = propInfo.GetValue(htmlAttributes);
        cssStyle = o.ToString().IsNullOrWhiteSpace() ? null : o.ToString();
    }

    var hTag = new TagBuilder("h" + hLevel);

    hTag.MergeAttribute("id", cssId);
    hTag.MergeAttribute("class", cssClass);
    hTag.MergeAttribute("style", cssStyle);
    hTag.InnerHtml = s;

    return new MvcHtmlString(hTag.ToString());
}

我发现尽管&#34;类&#34;和&#34;风格&#34;属性TagBuilder仍然将它们作为空字符串,例如&lt; h1 class =&#34;&#34;风格=&#34;&#34;&GT;但是对于id属性,它令人惊讶地起作用,因此当id的值为null时,标记中没有id属性。

我的问题 - 这种行为应该实际发生吗?如何使用TagBuilder实现缺少空值的属性?

我在VS2013,MVC 5中试过这个。

1 个答案:

答案 0 :(得分:0)

TagBuilder 会写入所有指定的属性,但空的或空的&#34; id&#34;属性。以下是在内部使用 TagBuilder 类的代码:

    private void AppendAttributes(StringBuilder sb)
    {
        foreach (var attribute in Attributes)
        {
            string key = attribute.Key;
            if (String.Equals(key, "id", StringComparison.Ordinal /* case-sensitive */) && String.IsNullOrEmpty(attribute.Value))
            {
                continue; // DevDiv Bugs #227595: don't output empty IDs
            }
            string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
            sb.Append(' ')
                .Append(key)
                .Append("=\"")
                .Append(value)
                .Append('"');
        }
    }

您可以像这样创建Html扩展程序:

    public static MvcHtmlString WriteHeader(this HtmlHelper html, string s, int? hLevel = 1, object htmlAttributes = null)
    {
        var hTag = new TagBuilder("h" + hLevel);
        hTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        hTag.InnerHtml = s;
        return MvcHtmlString.Create(hTag.ToString());
    }

,例如,可以按如下方式使用:

@Html.WriteHeader("hello1", 1)
@Html.WriteHeader("hello2", 2, new { @class = string.Empty, style = string.Empty })
@Html.WriteHeader("hello3", 3, new { id = "id3", @class = "css3", style = "style3" })

生成以下HTML代码:

<h1>hello1</h1>
<h2 class="" style="">hello2</h2>
<h3 class="css3" id="id3" style="style3">hello3</h3>