TagBuilder.MergeAttributes does not work
Asked Answered
I

2

6

I am creating my own helper in MVC. But the custom attributes are not added in the HTML:

Helper

public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes)
{
    var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    var builder = new TagBuilder("li");

    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase)
        && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        builder.AddCssClass("selected");

    if (htmlAttributes != null)
    {
        var attributes = new RouteValueDictionary(htmlAttributes);
        builder.MergeAttributes(attributes, false); //DONT WORK!!!
    }

    builder.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString();
    return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}

CSHTML

@Html.MenuItem("nossa igreja2", "Index", "Home", new { @class = "gradient-top" })

Final result (HTML)

<li class="selected"><a href="/">nossa igreja2</a></li>

Note that it did not add the class gradient-top that I mentioned in the helper call.

Intelligibility answered 22/6, 2011 at 14:15 Comment(0)
N
18

When calling MergeAttributes with replaceExisting set to false, it just adds the attributes that do not currently exist in the the attributes dictionary. It does not merge/concat the values of individual attributes.

I believe moving your call to

builder.AddCssClass("selected");

after

builder.MergeAttributes(attributes, false);

will fix your problem.

Neolithic answered 9/8, 2011 at 22:23 Comment(0)
Q
0

I wrote this extension method that does what I thought MergeAttributes was supposed to do (but upon checking the source code it just skips existing attributes):

public static class TagBuilderExtensions
{
    public static void TrueMergeAttributes(this TagBuilder tagBuilder, IDictionary<string, object> attributes)
    {
        foreach (var attribute in attributes)
        {
            string currentValue;
            string newValue = attribute.Value.ToString();

            if (tagBuilder.Attributes.TryGetValue(attribute.Key, out currentValue))
            {
                newValue = currentValue + " " + newValue;
            }

            tagBuilder.Attributes[attribute.Key] = newValue;
        }
    }
}
Quarto answered 5/2, 2014 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.