5

htmlhelper 拡張メソッドで同様に構造化された ctors をサポートする場合、htmlhelper 拡張を作成するときは、次のように使用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単にキャストできないことは知っていますが、理由はわかりませんが、それが私が混乱している場所かもしれません。ルーティングとは関係ないので、HtmlHelper メソッドとは関係ありませんか? 私が言うように、私はおそらく要点を見逃しているので、誰かが私が見逃したものを教えてくれたらうれしいです.htmlAttributesIDictionary<string, object>RouteValueDictionary

乾杯...

編集:ダンの答えに応えて -->

入力ヘルパーの 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()));
}

htmlAttribute.ToAttributeList()オブジェクトを

名前 = 「値」

これが理にかなっていることを願っています。

于 2009-03-26T00:15:08.517 に答える