2009-03-25 14 views
5

Al escribir una extensión HtmlHelper si quiero apoyar las ctors de estructura similar para mi método de extensión HtmlHelper, que utilizan RouteValueDictionary de la siguiente manera:métodos HtmlHelper y 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))); 
} 

Mi pregunta realmente es la razón por la necesidad de RouteValueDictionary. Sé que no puedes simplemente lanzar el htmlAttributes al IDictionary<string, object> ... aunque no estoy seguro de por qué y es posible que esté confundido. ¿No debería tener que ver RouteValueDictionary con el enrutamiento y, por lo tanto, no tiene nada que ver con los métodos de HtmlHelper? Como digo, probablemente me esté perdiendo el punto, así que me alegraría si alguien pudiera decirme qué me he perdido.

Saludos ...

edición: en respuesta a la respuesta de Dan ->

sólo estaba siguiendo lo que había visto en uso en el código fuente MVC para ayudantes de entrada ...

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

lo hace de la siguiente manera:

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

Claramente un acceso directo, pero ¿es una bastardización o está bien hacerlo?

Respuesta

5

Recomiendo encarecidamente mirar blog post de Rob Conery acerca de algo como esto.

La carne y las verduras de la misma es la siguiente:

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

Uso:

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

Lo .ToAttributeList() hace es convertir el objeto htmlAttribute a

name = "valor"

la esperanza que esto tiene sentido.

+0

Gracias Dan, eso parece interesante y voy a echar un vistazo. He editado mi pregunta porque no había dicho que estaba siguiendo lo que ya estaba en su lugar en los ayudantes de Framework mvc –

+2

Huh. ¡Estaba buscando esto otra vez para otro proyecto y encontré mi propia respuesta! ¡Que suerte! –

+0

+1 Gracias, me ayudó en un proyecto ... – xandercoded

Cuestiones relacionadas