我想在EditorTemplate上为Html.LabelFor添加一个css类
@Html.LabelFor(model => model.Name, new { @class = "myLabel" })
我的期望例如:label应该拿起css类.
为此我尝试使用以下代码扩展Label,但我的标签没有出现.我在这做错了什么?
public static class LabelExtensions
{
public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, object htmlAttributes)
{
return html.LabelFor(expression, null, htmlAttributes);
}
public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> 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 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(model => model.Name) ,那么它会显示标签.但我无法将css应用于我的标签.我希望在页面加载时以蓝色显示标签.然后使用jquey更改基于用户操作的标签样式.在我的页面上很好,除了我无法扩展并添加css类这一事实到我的标签.