5

在编写 htmlhelper 扩展时,如果我想为我的 htmlhelper 扩展方法支持类似结构的 ctor,我使用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)));
}

我的问题真的是为什么需要RouteValueDictionary......我知道你不能只是投到htmlAttributes......IDictionary<string, object>虽然我不确定为什么,这可能是我感到困惑的地方。不应该RouteValueDictionary与路由有关,因此与 HtmlHelper 方法无关?就像我说的那样,我可能错过了重点,所以如果有人能告诉我我错过了什么,我会很高兴。

干杯...

编辑:响应丹的回答-->

我只是按照我在输入帮助程序的 mvc 源代码中看到的内容......

  • 见“ src\SystemWebMvc\Mvc\Html\InputExtensions.cs

它的作用如下:

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

显然是一个捷径,但它是一个混蛋还是可以这样做?

4

1 回答 1

5

我强烈建议您查看 Rob Conery 的有关此类内容的博客文章

它的肉和蔬菜是这样的:

编码转储:

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

用法:

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

什么.ToAttributeList()是将您的 htmlAttribute 对象转换为

名称 = “价值”

希望这是有道理的。

于 2009-03-26T00:15:08.517 回答