2011-07-26 16 views
7

Uzantı yapıyorum. Ben denedimhtmlAtributes, uzantılamamda etiket oluşturucu ile birleşmiyor

The type arguments for method 'System.Web.Mvc.TagBuilder.MergeAttributes<TKey,TValue>(System.Collections.Generic.IDictionary<TKey,TValue>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 

:

public static MvcHtmlString Image(this HtmlHelper helper, string src, object htmlAttributes = null) { TagBuilder builder = new TagBuilder("img"); builder.MergeAttribute("src", src); if (htmlAttributes != null) builder.MergeAttributes(htmlAttributes); return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing)); } 

Bu hat: ile

if (htmlAttributes != null) builder.MergeAttributes(htmlAttributes); 

Hatalar

if (htmlAttributes != null) builder.MergeAttributes((Dictionary<string, string>)htmlAttributes); 

ve

if (htmlAttributes != null) builder.MergeAttributes((Dictionary<string, object>)htmlAttributes); 

Bunu nasıl çalıştırabilirim?

cevap

13

RouteValueDictionary numaralı telefonu kullanarak anonim bir yazıyı sözlüğe dönüştürmeniz gerekir. Bu kurucu, söz konusu nesneyi nesnenin özelliklerinden alır. Bu kurucu, sözlüğü nesnenin özelliklerinden alır.

25

Yeni RouteValueDictionary(htmlAttributes) yerine HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) kullanmak daha iyidir çünkü alt çizgi (ör. Data_click) ile yazılan veri çizgi niteliklerini destekler ve RouteValueDictionary üzerinde doğrudan bağımlılık yoktur.

+1

bu bana gerçekten yardımcı oldu - Bu gibi şeyler için etrafa baktım. –