我想在我的标签上添加一些课程,因此我在Label.cshtml文件夹下添加了Shared/EditorTemplates。似乎Razor无视它。我也试过了DisplayTemplates文件夹,但它也没有用。
我可以为标签设置编辑器模板吗?如果不是,那么定制它们的最佳方式是什么?
Label.cshtml:
@model object
@Html.LabelFor(m => m, new { @class = "col-xs-4 control-label" })
更新
这是我从this post获取的代码以及此useful link的一些更改。它仍然无法运作,我不知道发生了什么。有人可以帮忙吗?
public static class Extensions
{
public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, object htmlAttributes)
{
return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression)
{
return LabelFor(html, expression, null);
}
public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, IDictionary htmlAttributes){
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string FieldName = ExpressionHelper.GetExpressionText(expression);
string LabelText = metaData.DisplayName ?? metaData.PropertyName ?? FieldName.Split('.').Last();
if (string.IsNullOrEmpty(LabelText))
return MvcHtmlString.Empty;
TagBuilder tag = new TagBuilder("label");
tag.MergeAttributes(htmlAttributes);
tag.SetInnerText(LabelText);
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(FieldName));
tag.Attributes.Add("class", "control-label col-xs-2");
return MvcHtmlString.Create(tag.ToString());
}
}