HtmlHelper рендеринг словаря "ключи" и "значения" в качестве атрибутов - PullRequest
6 голосов
/ 20 марта 2012

Если вы считаете, что хотели бы получить дополнительную информацию, я ранее разместил и принял это. Правильное создание расширения ActionLink с помощью htmlAttributes

Это дополнительный вопрос. Моя проблема в том, что у меня есть два пользовательских расширения, одно из которых вызывает другое. По какой-то причине htmlAttributes рендерит так:

<a Count="3" 
 Keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]"
 Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]"
 ...</a>

Это происходит, когда я вызываю ActionImageLink2. Цель ActionImageLink2 состоит только в добавлении пар атрибутов. Как я могу сделать так, чтобы ключи и значения правильно отображались?

Что-то не так с HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)

Вот мои методы расширения

public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string altText, RouteValueDictionary routeValues, string _imageClass = "", object htmlAttributes = null)
        {

            var image = new TagBuilder("img");
            image.MergeAttribute("src", imageUrl);
            image.MergeAttribute("alt", altText);
            if (string.IsNullOrEmpty(_imageClass) == false) image.MergeAttribute("class", _imageClass);
            var link = helper.ActionLink("[replaceme]", routeValues, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

            return new MvcHtmlString(link.ToHtmlString().Replace("[replaceme]", image.ToString(TagRenderMode.SelfClosing)));
        }

        public static MvcHtmlString ActionImageLink2(this HtmlHelper helper, string imageUrl, string altText, RouteValueDictionary routeValues, string updateTarget, string _imageClass = "", object htmlAttributes = null)
        {
            RouteValueDictionary htmlAttr = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            htmlAttr.Add("data-ajaxget-click", "");
            htmlAttr.Add("data-ajax-update", (updateTarget.Contains("#") ? updateTarget : "#" + updateTarget));

            return helper.ActionImageLink(imageUrl, altText, routeValues, _imageClass, htmlAttr);
        }
        public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, RouteValueDictionary routeValues, object htmlAttributes = null)
        {
            return helper.ActionLink(linkText, routeValues["Action"].ToString(), routeValues["Controller"].ToString(), routeValues, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    }

Вот мой тест Посмотреть

У каждого ActionLink есть вторая строка, которая печатается как ToHtmlString. Печатный экран ниже показывает результат.

@{RouteValueDictionary route = new RouteValueDictionary();
            route["Area"] = "";
            route["Controller"] = "Test";
            route["Action"] = "Test";
}

@Html.ActionLink("Html.ActionLink", route, new { style = "color: green" })
<br />
@Html.ActionLink("Html.ActionLink", route, new { style = "color: green" }).ToHtmlString()
<br />
<br />
@Html.ActionImageLink("https://stackoverflow.com/users/flair/511438.png", "alt text", route, "image class", new { style = "background-color: orange" })
<br />
@Html.ActionImageLink("https://stackoverflow.com/users/flair/511438.png", "alt text", route, "image class", new { style = "background-color: orange" }).ToHtmlString()
<br />
<br />
@Html.ActionImageLink2("https://stackoverflow.com/users/flair/511438.png", "alt text", route, "partialDiv", "image class", new { style = "background-color: orange" })
<br />
@Html.ActionImageLink2("https://stackoverflow.com/users/flair/511438.png", "alt text", route, "partialDiv", "image class", new { style = "background-color: orange" }).ToHtmlString()

enter image description here

Ответы [ 2 ]

2 голосов
/ 20 марта 2012

Я бы сделал что-то вроде этого:

public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string altText, RouteValueDictionary routeValues, string _imageClass = "", IDictionary<String, object> htmlAttributes = null) 
{
    //...functionality omitted...
    var link = helper.ActionLink("[replaceme]", routeValues, htmlAttributes);
    //... other ....
}

public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string altText, RouteValueDictionary routeValues, string _imageClass = "", object htmlAttributes = null)
{
    return helper.ActionImageLink(imageUrl, altText, routeValues, _imageClass, new RouteValueDictionary(htmlAttributes));
}

public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, RouteValueDictionary routeValues, object htmlAttributes = null)
{
    return helper.ActionLink(linkText, routeValues, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes = null)
{
    return helper.ActionLink(linkText, routeValues["Action"].ToString(), routeValues["Controller"].ToString(), routeValues, htmlAttributes);
}

В двух словах: перегрузка

1 голос
/ 20 марта 2012

Проблема, я думаю, заключается в этой строке:

return helper.ActionImageLink(imageUrl, altText, routeValues, _imageClass, htmlAttr);

вашего метода ActionImageLink2. Когда вы передаете переменную htmlAttr методу ActionImageLink, он больше не является анонимным типом.

Таким образом, вы получаете проблему, когда в ActionImageLink вы звоните снова

HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)

Вы пытались проверить тип передачи перед вызовом вышеупомянутой функции?

...