Note that there are some explanatory texts on larger screens.

plurals
  1. POError converting anonymous (htmlAttributes) to IDictionary in custom helper
    text
    copied!<p>I'm trying to use a custom helper that creates a link using the example found at <a href="https://stackoverflow.com/questions/675319/is-there-an-asp-net-mvc-htmlhelper-for-image-links">Is there an ASP.NET MVC HtmlHelper for image links?</a>.</p> <p>Going on the assumption that this code actually functions, I'm not sure what the problem is. I'm new to anonymous types and MVC, so am assuming I'm missing something glaringly obvious.</p> <p>My code looks like such:</p> <pre><code> public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object imgHtmlAttributes) { UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; TagBuilder imgTag = new TagBuilder("img"); imgTag.MergeAttribute("src", imgSrc); imgTag.MergeAttributes((IDictionary&lt;string, string&gt;)imgHtmlAttributes, true); string url = urlHelper.Action(actionName, controllerName); TagBuilder imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); imglink.InnerHtml = imgTag.ToString(); return imglink.ToString(); } </code></pre> <p>The view code is this:</p> <pre><code>&lt;%= Html.ImageLink("../../imgs/details_icon", "View details", "Details", "Tanque", new { height = "5", width = "5" }) %&gt; </code></pre> <p>And the exception:</p> <pre><code>Unable to cast object of type '&lt;&gt;f__AnonymousType0`2[System.String,System.String]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.String]'. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload