2009-08-22 99 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中的代码中具有默认值的属性的注释。