Frage

Wenn Sie eine Htmlhelper Erweiterung schreiben, wenn ich die ähnlich strukturierte ctors für meine Htmlhelper-Erweiterungsmethode unterstützen wollen, verwende ich RouteValueDictionary wie folgt:

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ß, kann man nicht einfach die htmlAttributes werfen ... IDictionary<string, object> obwohl ich nicht sicher bin, warum und das könnte sein, wo ich bin verwirrt. Sollte nicht RouteValueDictionary sein mit Routing zu tun, und deshalb nichts mit Htmlhelper Methoden zu tun? Wie ich schon sagte, bin ich dabei wahrscheinlich den Punkt so dass ich froh wäre, wenn mir jemand sagen könnte, was ich verpasst habe.

Prost ...

edit: als Antwort auf Dan Antwort ->

Ich hatte nur nach dem, was ich in dem Einsatz in der mvc Quellcode für Eingang Helfer gesehen hatte ...

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

Es tut wie folgt:

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

Offensichtlich eine Verknüpfung aber ist es ein bastardization oder ist es in Ordnung, es zu tun?

War es hilfreich?

Lösung

würde ich dringend empfehlen, bei Rob Conery sucht Blog-Post über so etwas.

Das Fleisch und Gemüse davon ist diese:

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

Verwendung:

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"

Hoffe, dass dies Sinn macht.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top