gpt4 book ai didi

asp.net-mvc-3 - HtmlHelper 渲染字典 "keys"和 "values"作为属性

转载 作者:行者123 更新时间:2023-12-04 15:36:04 25 4
gpt4 key购买 nike

如果你觉得你想要进一步的背景,我之前已经发布并接受了这个。
Correctly making an ActionLink extension with 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));
}
}

这是我的测试 View

每个 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

最佳答案

我会做这样的事情:

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);
}

简而言之:重载

关于asp.net-mvc-3 - HtmlHelper 渲染字典 "keys"和 "values"作为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com