2009-03-25 5 views
5

Lors de l'écriture d'une extension HtmlHelper si je veux soutenir les cteurs de structure similaire pour ma HtmlHelper méthode d'extension, j'utilise RouteValueDictionary comme suit:méthodes de HtmlHelper et 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))); 
} 

Ma question est vraiment pourquoi la nécessité de RouteValueDictionary. .. Je sais que vous ne pouvez pas simplement lancer le htmlAttributes à IDictionary<string, object> ... bien que je ne sois pas sûr pourquoi et que ce soit là où je suis confus. Est-ce que RouteValueDictionary ne devrait pas avoir à faire avec Routing et donc rien à voir avec les méthodes HtmlHelper? Comme je le dis, je manque probablement le point si je serais heureux si quelqu'un pourrait me dire ce que j'ai manqué.

... Vive

edit: en réponse à la réponse de Dan ->

Je ne faisais que suivre ce que j'avais vu utilisé dans le code source pour les aides mvc d'entrée ...

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

Il ne comme suit:

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

De toute évidence un raccourci, mais est-ce une bâtardisation ou est-ce acceptable?

Répondre

5

Je recommande fortement de regarder blog post de Rob Conery à propos de quelque chose comme ça.

La viande et les légumes de c'est ceci:

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

Utilisation:

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

Qu'est-ce que .ToAttributeList() n'est de convertir votre objet htmlAttribute à

name = "valeur"

Hope this sens.

+0

Merci Dan, ça a l'air intéressant et je vais jeter un oeil. J'ai édité ma question parce que je n'avais pas dit que je suivais ce qui était déjà en place dans les aides du framework mvc –

+2

Huh. Je le cherchais encore pour un autre projet et j'ai trouvé ma propre réponse! Quelle chance! –

+0

+1 Merci, m'a aidé sur un projet ... – xandercoded

Questions connexes