我已经为 Html Helper 库编写了一个小的扩展方法,如下所示:
public static MvcHtmlString Image(this HtmlHelper htmlHelper, string url, string alt)
{
var img = new TagBuilder("img");
img.MergeAttribute("src", url);
img.MergeAttribute("alt", alt);
return new MvcHtmlString(img.ToString(TagRenderMode.SelfClosing));
}
我想更改它,以便可以根据需要添加参数。就像是:
public static MvcHtmlString Image(this HtmlHelper htmlHelper, string url, object htmlParameters)
{
var img = new TagBuilder("img");
img.MergeAttribute("src", url);
//Now I would like to extract the properties (and their values) from the object
//and add them to img.
return new MvcHtmlString(img.ToString(TagRenderMode.SelfClosing));
}
我想让这个函数通用并传递一个object
自定义属性并将这些属性应用于img
标签。就像我们传递htmlParameters
toActionLink
辅助方法一样。
@Html.ActionLink("link text", "actionName","controllerName", new {@class = "class"})
这有什么诀窍?