2009-08-22 97 views
23

在asp.net mvc中,我總是看到內置的html助手,他們總是有對象htmlAttirbutes。如何從對象中獲取值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更有意義。 – 2009-10-07 20:46:55

+2

這似乎不適用於像data_bind這樣的屬性 – SimonGates 2013-05-22 12:05:50

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_id轉換爲data-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中的代碼中具有默認值的屬性的註釋。