2009-03-25 13 views
5

Wenn ein Htmlhelper-Erweiterung zu schreiben, wenn ich die ähnlich strukturierte ctors für meine Htmlhelper-Erweiterungsmethode unterstützen wollen, verwende ich RouteValueDictionary wie folgt:Htmlhelper Methoden und Routevaluedictionary

public static string ListBoxDict(this HtmlHelper htmlHelper, 
           string name, 
           object value, 
           object htmlAttributes) 
{ 
    return ListBoxDict(htmlHelper, 
         name, 
         value, 
         ((IDictionary<string, object>) 
          new RouteValueDictionary(htmlAttributes))); 
} 

Meine Frage ist wirklich, warum die Notwendigkeit für RouteValueDictionary. .. Ich weiß, dass Sie nicht einfach die htmlAttributes zu IDictionary<string, object> casten können ... obwohl ich nicht sicher bin, warum und das könnte sein, wo ich verwirrt bin. Sollte nicht RouteValueDictionary mit Routing zu tun haben und daher nichts mit HtmlHelper-Methoden zu tun haben? Wie ich schon sagte, ich verpasse wahrscheinlich den Punkt, also wäre ich froh, wenn mir jemand sagen könnte, was ich verpasst habe.

Prost ...

edit: als Antwort Antwort auf Dans ->

Ich war gerade nach dem, was ich in dem Einsatz in der mvc Quellcode für die Eingabe Helfer ...

gesehen hatte
  • siehe "src\SystemWebMvc\Mvc\Html\InputExtensions.cs"

Es ist wie folgt:

public static string TextBox(this HtmlHelper htmlHelper, 
          string name, 
          object value, 
          object htmlAttributes) 
{ 
    return TextBox(htmlHelper, 
        name, 
        value, 
        new RouteValueDictionary(htmlAttributes)) 
} 

Klar eine Abkürzung, aber ist es eine Bastardisierung oder ist es in Ordnung, es zu tun?

Antwort

5

Ich würde empfehlen, Rob Conery blog post über so etwas zu betrachten.

Das Fleisch und Gemüse davon ist dies:

Codedump:

public static string ToAttributeList(this object list) 
{ 
    StringBuilder sb = new StringBuilder(); 
    if (list != null) 
    { 
    Hashtable attributeHash = GetPropertyHash(list); 
    string resultFormat = "{0}=\"{1}\" "; 
    foreach (string attribute in attributeHash.Keys) 
    { 
     sb.AppendFormat(resultFormat, attribute.Replace("_", ""), 
      attributeHash[attribute]); 
    } 
    } 
    return sb.ToString(); 
} 

public static string ToAttributeList(this object list, 
            params object[] ignoreList) 
{ 
    Hashtable attributeHash = GetPropertyHash(list); 

    string resultFormat = "{0}=\"{1}\" "; 
    StringBuilder sb = new StringBuilder(); 
    foreach (string attribute in attributeHash.Keys) 
    { 
    if (!ignoreList.Contains(attribute)) 
    { 
     sb.AppendFormat(resultFormat, attribute, 
      attributeHash[attribute]); 
    } 
    } 
    return sb.ToString(); 
} 

public static Hashtable GetPropertyHash(object properties) 
{ 
    Hashtable values = null; 

    if (properties != null) 
    { 
    values = new Hashtable(); 
    PropertyDescriptorCollection props = 
     TypeDescriptor.GetProperties(properties); 

    foreach (PropertyDescriptor prop in props) 
    { 
     values.Add(prop.Name, prop.GetValue(properties)); 
    } 
    } 
    return values; 
} 

Verbrauch:

public static string ListBoxDict(this HtmlHelper htmlHelper, 
           string name, 
           object value, 
           object htmlAttributes) 
{ 
    return htmlHelper.ListBoxDict(name, 
            value, 
            htmlAttributes.ToAttributeList())); 
} 

Was .ToAttributeList() tut, ist Ihr htmlAttribute Objekt

konvertieren

name = "Wert"

Hoffnung das macht Sinn.

+0

Danke Dan, das sieht interessant aus und ich werde einen Blick darauf werfen. Ich habe meine Frage bearbeitet, weil ich nicht gesagt hatte, dass ich dem folgte, was bereits im mvc-Framework-Helfer –

+2

Huh war. Ich war wieder auf der Suche nach einem anderen Projekt und habe meine eigene Antwort gefunden! Was für ein Glück! –

+0

+1 Danke, half mir auf ein Projekt ... – xandercoded

Verwandte Themen