metodi HtmlHelper e RouteValueDictionary
-
22-08-2019 - |
Domanda
Quando si scrive un prolungamento HtmlHelper se voglio sostenere le ctors analoga struttura per il mio metodo di estensione HtmlHelper, io uso RouteValueDictionary
come segue:
public static string ListBoxDict(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return ListBoxDict(htmlHelper,
name,
value,
((IDictionary<string, object>)
new RouteValueDictionary(htmlAttributes)));
}
La mia domanda è in realtà il motivo per cui la necessità di RouteValueDictionary
... lo so che non si può semplicemente gettare la htmlAttributes
a IDictionary<string, object>
... anche se non so perché e che potrebbe essere dove sono confuso. Non dovrebbe essere RouteValueDictionary
a che fare con Routing e quindi nulla a che fare con i metodi HtmlHelper? Come ho detto, io sono probabilmente manca il punto in modo sarei felice se qualcuno potrebbe dirmi quello che ho perso.
Saluti ...
modifica: in risposta alla risposta di Dan ->
Stavo solo seguendo quello che avevo visto in uso nel codice sorgente MVC per aiutanti di ingresso ...
- vedi "
src\SystemWebMvc\Mvc\Html\InputExtensions.cs
"
Lo fa nel seguente modo:
public static string TextBox(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return TextBox(htmlHelper,
name,
value,
new RouteValueDictionary(htmlAttributes))
}
Chiaramente una scorciatoia, ma si tratta di un imbastardimento o è ok per farlo?
Soluzione
consiglio vivamente guardando Rob Conery di post sul blog su qualcosa di simile.
La carne e verdure di esso è questo:
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;
}
Utilizzo:
public static string ListBoxDict(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return htmlHelper.ListBoxDict(name,
value,
htmlAttributes.ToAttributeList()));
}
Che .ToAttributeList()
non fa altro che convertire il vostro oggetto htmlAttribute a
name = "valore"
Spero che questo ha un senso.