title: Extend MVC3 razor Html.LabelFor to add css class
body: <p>I am trying to add a css class to Html.LabelFor   on an EditorTemplate</p>
  
  <pre><code> @Html.LabelFor(model =&gt; model.Name, new { @class = "myLabel" })
  </code></pre>
  
  <p>my expectation for instance:label should pick up the css class.</p>
  
  <p>For this I tried to extend Label with the following code ,but my label is not showing up .
  What am I doing wrong here ?</p>
  
  <pre><code> public static class LabelExtensions
      {
          public static MvcHtmlString LabelFor&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression, object htmlAttributes)
          {
              return html.LabelFor(expression, null, htmlAttributes);
          }
  
          public static MvcHtmlString LabelFor&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression, string labelText, object htmlAttributes)
          {
              return html.LabelHelper(
                  ModelMetadata.FromLambdaExpression(expression, html.ViewData),
                  ExpressionHelper.GetExpressionText(expression),
                  HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
                  labelText);
          }
  
          private static MvcHtmlString LabelHelper(this HtmlHelper html, ModelMetadata metadata, string htmlFieldName, IDictionary&lt;string, object&gt; htmlAttributes, string labelText = null)
          {
              var str = labelText
                  ?? (metadata.DisplayName
                  ?? (metadata.PropertyName
                  ?? htmlFieldName.Split(new[] { '.' }).Last()));
  
              if (string.IsNullOrEmpty(str))
                  return MvcHtmlString.Empty;
  
              var tagBuilder = new TagBuilder("label");
              tagBuilder.MergeAttributes(htmlAttributes);
              tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
              tagBuilder.SetInnerText(str);
  
              return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
          }
  
          private static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode)
          {
              return new MvcHtmlString(tagBuilder.ToString(renderMode));
          }
      }
  </code></pre>
  
  <p>If I am not specifying css class for example    <code>@Html.LabelFor(model =&gt; model.Name)</code>   ,then it shows the label.
  But I am not able to apply css to my label.
  I want to show the label in blue color, when the page loads.,then to change the label style based on user action using jquey.All is fine,on my page except the fact that I am unable to extend and add a css class to my label.</p>