2009-08-22 11 views
23

asp.net mvcでは、常にhtmlAttirbutesオブジェクトを持つ組み込みのHTMLヘルパーが表示されます。オブジェクトの値を取得する方法HtmlAttributes

私は通常、新しい{@id = "test"、@ class = "myClass"}を実行します。

このようなパラメータを自分のhtmlヘルパーでどのように抽出するのですか?

私は "HtmlTextWriterTag"を使っています。私はこのオブジェクト全体をライターに渡すことができます。

また、これは大きなhtmlヘルパーでどのように機能しますか?

私はHTMLヘルパーを作成していますし、これらのタグをすべて使用しています。

Table 
thead 
tfooter 
tbody 
tr 
td 
a 
img 

これらのタグのそれぞれにhtml属性を設定する必要がありますか?

答えて

36

私は通常、このような何か:

public static string Label(this HtmlHelper htmlHelper, string forName, string labelText, object htmlAttributes) 
    { 
     return Label(htmlHelper, forName, labelText, new RouteValueDictionary(htmlAttributes)); 
    } 

    public static string Label(this HtmlHelper htmlHelper, string forName, string labelText, 
           IDictionary<string, object> htmlAttributes) 
    { 
     // Get the id 
     if (htmlAttributes.ContainsKey("Id")) 
     { 
      string id = htmlAttributes["Id"] as string; 
     } 

     TagBuilder tagBuilder = new TagBuilder("label"); 
     tagBuilder.MergeAttributes(htmlAttributes); 
     tagBuilder.MergeAttribute("for", forName, true); 
     tagBuilder.SetInnerText(labelText); 
     return tagBuilder.ToString(); 
    } 

を私はあなたがCodePlexのからASP.NET MVCのソースをダウンロードして、HTMLヘルパーに建てを見てみることをお勧めします。

+0

あなたの例は、ソースよりも有益です。 IDictionary(ラベルのように)が期待されていたように、ソースがどのように動作していたのかわかりませんでしたが、匿名オブジェクトに渡そうとしていました。一度私はあなたがそれをRouteValueDictionaryに変換するのを見た方がより意味がある。 –

+2

これはdata_bindのような属性では機能しないようです – SimonGates

3

あなたは、このように属性/値の文字列表現にオブジェクトhtmlAttirbutesを変換することができます。

var htmlAttributes = new { id="myid", @class="myclass" }; 

string string_htmlAttributes = ""; 
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) 
{ 
    string_htmlAttributes += string.Format("{0}=\"{1}\" ", property.Name.Replace('_', '-'), property.GetValue(htmlAttributes)); 
} 

PropertyDescriptor

System.ComponentModel

1

が、私は両方の方法(Chtiwiマレクとrrejc)のミックスを使用したクラスに属しています先に提案され、それは素晴らしい作品です。

この方法では、data_iddata-idに変換します。以前設定したデフォルトの属性値も上書きします。

using System.ComponentModel; 
... 


public static MvcHtmlString RequiredLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes) 
{ 
    var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 

    string htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
    string labelText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last(); 

    if (String.IsNullOrEmpty(labelText)) 
     return MvcHtmlString.Empty; 

    var label = new TagBuilder("label"); 
    label.Attributes.Add("for", helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); 

    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(htmlAttributes)) 
    { 
     // By adding the 'true' as the third parameter, you can overwrite whatever default attribute you have set earlier. 
     label.MergeAttribute(prop.Name.Replace('_', '-'), prop.GetValue(htmlAttributes).ToString(), true); 
    } 
    label.InnerHtml = labelText; 
    return MvcHtmlString.Create(label.ToString()); 
} 

デフォルト値を持つ属性をforeach内のコードに上書きすることについてのコメントに注意してください。

関連する問題