2015-06-01 69 views
0

我試圖從this關注自定義圖像鏈接助手的堆棧溢出問題。代碼起作用,只要我刪除.MergeAttributes命令。當使用時,該命令鼓起,投擲以下異常無法投射'<> f__AnonymousType1`1 [System.String]'類型的對象來鍵入

無法轉換類型 '<> f__AnonymousType1 1[System.String]' to type 'System.Collections.Generic.IDictionary 2 [System.String,System.String]' 的對象。

下面是我使用的helper類的代碼。這個想法是隻使用兩個字符串值作爲輸入參數,而任何其他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"}) 

回答

1

您可以使用HtmlHelper的​​方法來投你的對象。與

imgTag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(imgAttributes), true); 
imgLink.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true); 
+0

優秀的以下行

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

,謝謝! – NealR

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."); 
    } 
相關問題