14

I am trying to add a css class to Html.LabelFor on an EditorTemplate

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

my expectation for instance:label should pick up the css class.

For this I tried to extend Label with the following code ,but my label is not showing up . What am I doing wrong here ?

 public static class LabelExtensions
    {
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            return html.LabelFor(expression, null, htmlAttributes);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> 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<string, object> 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));
        }
    }

If I am not specifying css class for example @Html.LabelFor(model => model.Name) ,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.

1
  • 1
    Please define "not working". What is the result you expect, and what is the result you receive? Commented May 15, 2012 at 14:34

2 Answers 2

16

I suspect that you forgot to bring the namespace in which you defined this LabelExtensions class in scope in the view. So for example if this class is defined inside the MyProject.Extensions namespace:

namespace MyProject.Extensions
{
    public static class LabelExtensions
    {
        ...
    }
}

make sure that you've brought this namespace into scope:

@using MyProject.Extensions
@model MyViewModel
...
@Html.LabelFor(model => model.Name, new { @class = "myLabel" })
Sign up to request clarification or add additional context in comments.

Comments

0

You're using html.LabelHelper

However you have defined your own LabelHelper method, so use it =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.