2012-03-27 11 views
11

Html.Label i Html.LabelFor metody pomocnicze nie obsługują parametru htmlAttributes tak jak większość innych pomocników. Chciałbym jednak ustawić class. Coś takiego:Jak rozszerzyć pomocników HTML LabelMore i LabelFor HTML?

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

Czy istnieje łatwy sposobem zwiększenia Etykieta/LabelFor bez uciekania się do kopiowania i przedłużające wyjście disasm ILSpy tych metod?

Odpowiedz

25

można łatwo rozszerzyć etykietę tworząc własną LabelFor:

Coś jak to powinien robić to, co trzeba

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)); 
} 

Aktualizacja Aby użyć metody rozszerzenie właśnie utworzonego w projekcie, dodać ten wiersz w swoim 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

wiem, ale staram się unikać replikacji demontażu/Original wyjścia źródła. Oryginalny kod składa się z 22 metod, zrób to poprawnie, musiałbym je wszystkie zastąpić. – batkuip

+2

nie. po prostu utwórz projekt rozszerzenia i wstaw do swojego web.config wywołanie do swojego projektu. – Iridio

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)); 
    } 
} 

, a następnie:

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

czyli

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

Przepraszam, kolego, gdybym się nie wyjaśnił. Zrobiłem to (skopiowałem źródło MVC i rozszerzyłem je), ale jest to coś, czego chciałbym uniknąć z wielu powodów. Poza tym lubię dbać o porządek. Miałem nadzieję, że będzie coś bardziej eleganckiego. – batkuip

+1

Niestety, nie ma tam niczego, co pozwoli ci to zrobić. Wbudowany helper nie jest rozszerzalny i ma atrybuty na stałe. –

+0

Jak mogę skompilować kompilator między myProject.Helpers.LabelExtensions.LabelFor a "System.Web.Mvc.Html.LabelExtensions.LabelFor (potrzebne do innych rzeczy na stronie)? – Ungaro

Powiązane problemy