I'm trying to follow the answer from this Stack Overflow question regarding a custom Image Link helper. The code works, as long as I remove the .MergeAttributes
command. When used, this command blows up, throwing the following exception
Unable to cast object of type '<>f__AnonymousType1
1[System.String]' to type 'System.Collections.Generic.IDictionary
2[System.String,System.String]'.
Below is the code from the helper class I'm using. The idea was to only use two string values as input parameters, and any other HTML/img tag attributes input as properties of the input objects.
public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string url, object imgAttributes, object htmlAttributes)
{
UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
var imgTag = new TagBuilder("img");
imgTag.MergeAttribute("src", imgSrc);
imgTag.MergeAttributes((IDictionary<string, string>)imgAttributes, true); //Exception thrown here
var imgLink = new TagBuilder("a");
imgLink.MergeAttribute("href", url);
imgLink.InnerHtml = imgTag.ToString();
imgLink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);
return MvcHtmlString.Create(imgLink.ToString());
}
And here is the code from the Razor/.cshtml file.
@Html.ImageLink(Url.Content("~/Content/images/Screen.PNG"), System.Configuration.ConfigurationManager.AppSettings["Periscope"],
new {title="Search Periscope"} , new { target="_blank"})