Val*_*mas 6 extension-methods html-helper asp.net-mvc-3
如果您觉得您想要更多背景知识,我之前已经发布并接受了这一点. 使用htmlAttributes正确制作ActionLink扩展
这是一个后续问题.我的问题是我有两个自定义扩展,其中一个调用另一个.出于某种原因,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>
Run Code Online (Sandbox Code Playgroud)
当我调用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));
}
}
Run Code Online (Sandbox Code Playgroud)
每个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()
Run Code Online (Sandbox Code Playgroud)

我会做这样的事情:
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);
}
Run Code Online (Sandbox Code Playgroud)
简而言之:超载
| 归档时间: |
|
| 查看次数: |
3563 次 |
| 最近记录: |