html如何自定义lable的位置,自定义Html.LabelFor

我想在我的标签上添加一些课程,因此我在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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值