Asp.net mvc 3 扩展MVC3 razor Html.LabelFor以添加css类
我正在尝试向EditorTemplate上的Html.LabelFor添加css类Asp.net mvc 3 扩展MVC3 razor Html.LabelFor以添加css类,asp.net-mvc-3,c#-4.0,razor,Asp.net Mvc 3,C# 4.0,Razor,我正在尝试向EditorTemplate上的Html.LabelFor添加css类 @Html.LabelFor(model => model.Name, new { @class = "myLabel" }) 例如,我的期望是:标签应该选择css类 为此,我尝试用以下代码扩展标签,但我的标签没有显示出来。 我做错了什么 public static class LabelExtensions { public static MvcHtmlString Labe
@Html.LabelFor(model => model.Name, new { @class = "myLabel" })
例如,我的期望是:标签应该选择css类
为此,我尝试用以下代码扩展标签,但我的标签没有显示出来。
我做错了什么
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));
}
}
公共静态类标签扩展
{
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、对象htmlAttributes)
{
返回html.LabelFor(表达式,null,htmlAttributes);
}
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、字符串labelText、对象htmlAttributes)
{
返回html.LabelHelper(
ModelMetadata.FromLambdaExpression(表达式,html.ViewData),
ExpressionHelper.GetExpressionText(表达式),
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
标签文本);
}
私有静态MvcHtmlString LabelHelper(此HtmlHelper html、ModelMetadata元数据、字符串htmlFieldName、IDictionary htmlAttributes、字符串labelText=null)
{
var str=labelText
??(metadata.DisplayName)
??(metadata.PropertyName)
??htmlFieldName.Split(新[]{'.}.Last());
if(string.IsNullOrEmpty(str))
返回MvcHtmlString.Empty;
var tagBuilder=新的tagBuilder(“标签”);
tagBuilder.MergeAttributes(HtmlatAttributes);
tagBuilder.Attributes.Add(“for”,tagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName));
tagBuilder.SetInnerText(str);
返回tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
}
私有静态MvcHtmlString-ToMvcHtmlString(此TagBuilder TagBuilder,TagRenderMode renderMode)
{
返回新的MvcHtmlString(tagBuilder.ToString(renderMode));
}
}
如果我没有指定css类,例如@Html.LabelFor(model=>model.Name)
,那么它会显示标签。
但我无法将css应用于我的标签。
我想在页面加载时以蓝色显示标签,然后使用jquey根据用户操作更改标签样式。在我的页面上,一切都很好,只是我无法扩展css类并将其添加到标签中。我怀疑您忘了将定义此LabelExtensions类的命名空间引入视图的作用域。例如,如果此类是在
MyProject.Extensions
命名空间中定义的:
namespace MyProject.Extensions
{
public static class LabelExtensions
{
...
}
}
确保已将此命名空间引入范围:
@using MyProject.Extensions
@model MyViewModel
...
@Html.LabelFor(model => model.Name, new { @class = "myLabel" })
您正在使用html.LabelHelper 但是,您已经定义了自己的LabelHelper方法,因此请使用它=)请定义“不工作”。你期望的结果是什么,你得到的结果是什么?