【问题标题】:HtmlHelper methods and RouteValueDictionaryHtmlHelper 方法和 RouteValueDictionary
【发布时间】:2009-03-25 23:36:12
【问题描述】:

在编写 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&lt;string, object&gt; ...尽管我不确定为什么,这可能就是我的位置米糊涂。 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))
}

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

【问题讨论】:

    标签: asp.net-mvc html-helper


    【解决方案1】:

    我强烈建议您查看 Rob Conery 的 blog post 关于此类的内容。

    它的肉和蔬菜是这样的:

    编码转储:

    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 对象转换为

    名称 = “价值”

    希望这是有道理的。

    【讨论】:

    • 谢谢 Dan,这看起来很有趣,我会看看。我已经编辑了我的问题,因为我没有说我正在关注 mvc 框架帮助程序中已经存在的内容
    • 嗯。我再次为另一个项目寻找这个并找到了我自己的答案!真幸运!
    • @GoldBishop 修复了链接。 Wayback 机器意外地频繁帮助断开链接...
    • @CristiDiaconescu 谢谢!该博客现在位于子域上,所以我现在已经更正了。 :)
    • 酷! @RobConery Tsk tsk tsk,路由重定向;)
    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2010-10-27
    相关资源
    最近更新 更多