Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend MVC3 razor Html.LabelFor to add css class

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.

like image 311
Millar Avatar asked May 15 '12 14:05

Millar


1 Answers

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" })
like image 103
Darin Dimitrov Avatar answered Nov 07 '22 20:11

Darin Dimitrov