2012-03-27 73 views
11

Html.LabelHtml.LabelFor幫助程序方法不像大多數其他幫助程序那樣支持htmlAttributes參數。但是我想設置class。事情是這樣的:如何擴展MVC3標籤和LabelFor HTML助手?

Html.LabelFor(model => model.Name, new { @class = "control-label" }) 

是否有擴展標籤/ LabelFor 沒有訴諸複製和擴大這些方法的ILSpy DISASM輸出的容易方式?

回答

25

,您可以輕鬆創建自己的LabelFor延長標籤:

像這樣的東西應該做的,你需要

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) 
{ 
    return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes)); 
} 

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) 
{ 
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); 
    string htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
    string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); 
    if (String.IsNullOrEmpty(labelText)) 
    { 
    return MvcHtmlString.Empty; 
    } 

    TagBuilder tag = new TagBuilder("label"); 
    tag.MergeAttributes(htmlAttributes); 
    tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); 
    tag.SetInnerText(labelText); 
    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); 
} 

更新 在使用時只需在你的項目中創建的擴展方法是什麼,將此行加入您的Views\web.config

<pages> 
    <namespaces> 
    <add namespace="System.Web.Helpers" /> 
    <add namespace="System.Web.Mvc" /> 
    <add namespace="System.Web.Mvc.Ajax" /> 
    <add namespace="System.Web.Mvc.Html" /> 
    <add namespace="System.Web.Routing" /> 
    <add namespace="System.Web.WebPages" /> 
    <add namespace="MyProject.Helpers" /> <-- my extension 

    ... 

</namespaces> 
</pages> 
+0

我知道,但我想避免複製拆卸/原始源輸出。原始代碼由22個方法組成,正確執行此操作,我將不得不重寫它們。 – batkuip 2012-03-27 08:19:12

+2

沒有。您只需創建您的擴展項目並在您的web.config中插入對您的項目的調用。 – Iridio 2012-03-27 08:23:57

6
public static class LabelExtensions 
{ 
    public static IHtmlString LabelFor<TModel, TProperty>(
     this HtmlHelper<TModel> htmlHelper, 
     Expression<Func<TModel, TProperty>> expression, 
     string labelText, 
     object htmlAttributes 
    ) 
    { 
     var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData); 
     var htmlFieldName = ExpressionHelper.GetExpressionText(expression); 
     return LabelHelper(htmlHelper, metadata, htmlFieldName, labelText, htmlAttributes); 
    } 

    public static IHtmlString Label(this HtmlHelper htmlHelper, string expression, string labelText, object htmlAttributes) 
    { 
     var metadata = ModelMetadata.FromStringExpression(expression, htmlHelper.ViewData); 
     return LabelHelper(htmlHelper, metadata, expression, labelText, htmlAttributes); 
    } 

    private static IHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText, object htmlAttributes) 
    { 
     string str = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>())); 
     if (string.IsNullOrEmpty(str)) 
     { 
      return MvcHtmlString.Empty; 
     } 
     TagBuilder tagBuilder = new TagBuilder("label"); 
     tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); 
     var attributes = new RouteValueDictionary(htmlAttributes); 
     tagBuilder.MergeAttributes(attributes); 
     tagBuilder.SetInnerText(str); 
     return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)); 
    } 
} 

然後:

@Html.LabelFor(x => x.SomeProperty, null, new { @class = "foo" }) 

或:

@Html.Label("SomeProperty", null, new { @class = "foo" }) 
+0

對不起,如果我沒有讓自己清楚。我已經完成了這個(複製MVC源代碼並將其擴展),但是由於多種原因,我想避免這種情況。我也喜歡讓事情乾燥。我希望能有更優雅的東西。 – batkuip 2012-03-27 08:43:39

+1

對不起,沒有什麼可以讓你做到這一點。內置的幫助程序不可擴展,並且具有硬編碼的屬性。 – 2012-03-27 09:03:13

+0

如何讓編譯器在myProject.Helpers.LabelExtensions.LabelFor和'System.Web.Mvc.Html.LabelExtensions.LabelFor(頁面上的其他東西需要)之間消除歧義? – Ungaro 2013-12-20 03:42:22