无法将类型为'<> f__AnonymousType1`1 [System.String]'的对象转换为类型

时间:2015-06-01 21:48:42

标签: asp.net-mvc-4

我正在尝试关注有关自定义图像链接助手的this Stack Overflow问题的答案。只要我删除.MergeAttributes命令,代码就可以工作。使用时,此命令会爆炸,抛出以下异常

  

无法转换类型为'<> f__AnonymousType1 1[System.String]' to type 'System.Collections.Generic.IDictionary 2 [System.String,System.String]'的对象。

以下是我正在使用的帮助程序类的代码。我们的想法是只使用两个字符串值作为输入参数,并将任何其他HTML / img标记属性作为输入对象的属性输入。

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

这是Razor / .cshtml文件中的代码。

@Html.ImageLink(Url.Content("~/Content/images/Screen.PNG"), System.Configuration.ConfigurationManager.AppSettings["Periscope"], 
                               new {title="Search Periscope"} , new { target="_blank"})

2 个答案:

答案 0 :(得分:1)

您可以使用AnonymousObjectToHtmlAttributes()的{​​{1}}方法投射对象。替换以下行

HtmlHelper

imgTag.MergeAttributes((IDictionary<string, string>)imgAttributes, true);
imgLink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

答案 1 :(得分:0)

你投射的方式似乎没有效果。

使用以下扩展方法使其正常工作。

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);
        var color = imgAttributes.ToDictionary<string>();
        imgTag.MergeAttributes(color, true); //No moer Exception thrown here

        var imgLink = new TagBuilder("a");
        imgLink.MergeAttribute("href", url);
        imgLink.InnerHtml = imgTag.ToString();
        imgLink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

        return MvcHtmlString.Create(imgLink.ToString());
    }
    public static IDictionary<string, object> ToDictionary(this object source)
    {
        return source.ToDictionary<object>();
    }

    public static IDictionary<string, T> ToDictionary<T>(this object source)
    {
        if (source == null)
            ThrowExceptionWhenSourceArgumentIsNull();

        var dictionary = new Dictionary<string, T>();
        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
            AddPropertyToDictionary<T>(property, source, dictionary);
        return dictionary;
    }

    private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary)
    {
        object value = property.GetValue(source);
        if (IsOfType<T>(value))
            dictionary.Add(property.Name, (T)value);
    }

    private static bool IsOfType<T>(object value)
    {
        return value is T;
    }

    private static void ThrowExceptionWhenSourceArgumentIsNull()
    {
        throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
    }