扩展MVC3剃刀Html.LabelFor添加CSS类 [英] Extend MVC3 razor Html.LabelFor to add css class

查看:191
本文介绍了扩展MVC3剃刀Html.LabelFor添加CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个CSS类添加到Html.LabelFor上EditorTemplate

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

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

我的期望例如:标签应该拿起的CSS类

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

有关这个我试着用下面的code扩展标签,但我的标签没有显示出来。
我在做什么错在这里?

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

如果我不指定例如CSS类 @ Html.LabelFor(型号=&GT; model.Name),那么它显示的标签。
但我不能够应用CSS到我的标签。
我想显示蓝色的标签,当页面加载,然后根据用户操作使用jquey.All是好的改变标签的风格,我的网页上,除了一个事实,我无法扩展,并添加CSS类我的标签。

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.

推荐答案

我怀疑你忘记带在你的范围在视图中定义这个LabelExtensions类的命名空间。因此,举例来说,如果这个类的 MyProject.Extensions 命名空间中定义的:

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

这篇关于扩展MVC3剃刀Html.LabelFor添加CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆