【问题标题】:Customizing Html.LabelFor自定义 Html.LabelFor
【发布时间】:2015-05-19 05:35:13
【问题描述】:

我想给我的标签添加一些类,所以我在Shared/EditorTemplates 文件夹下添加了Label.cshtml。 Razor 似乎忽略了它。我也试过DisplayTemplatesfolder,但也没有用。

我可以有标签的编辑器模板吗?如果不是,自定义它们的最佳方式是什么?

标签.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<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
    {
        return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        return LabelFor(html, expression, null);
    }
    public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> 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());
    }
}

【问题讨论】:

  • EditorTemplate's 基于属性的类型(而不是基于它们生成的 html 元素)。您需要创建自己的 html 助手

标签: asp.net-mvc razor mvc-editor-templates


【解决方案1】:

EditorTemplate 是基于属性的类型(而不是基于它们生成的 html 元素)。您需要创建自己的 html 帮助程序

public static MvcHtmlString BootstrapLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
  var attributes = new { @class = "col-xs-4 control-label" };
  return helper.LabelFor(expression, attributes);
}

并在视图中使用它作为

@Html.BootstrapLabelFor(m => m.yourProperty)

然而,这似乎是一种不灵活的方式来使用帮助程序,只是为了节省在标准 LabelFor() 帮助程序中添加 html 属性。根据this answer 将关联的文本框和验证消息组合在一个帮助器中可能会更有用(请注意,此答案还显示了如何将命名空间添加到web.config 文件

【讨论】:

  • 谢谢斯蒂芬,但现在我正在尝试为 labelfor 添加扩展,所以希望我不必更改我的代码。
  • 什么意思?这一个扩展方法(呈现带有类名的标签)
  • 我的意思是我想在某种程度上覆盖Labelfor 的默认行为以在其中添加我的类。请查看我在问题底部附上的代码。
  • 这正是它的作用(也是唯一的方法)
  • 除非你给它一个完全不同的、明确的签名,或者完全限定方法名称,例如对于标准助手,您需要使用@System.Web.Mvc.Html.LabelExtensions.LabelFor(Html, m =&gt; m.YourProperty),并且您需要根据您的程序集和类名称执行类似的操作。 (您最终输入的内容比使用内置帮助程序的内容更多)
【解决方案2】:

EditorTemplate and DisplayTemplate 基于属性类型。您可以通过 creating your own HTML helper 执行此操作。

public static class LabelExtensions
 {
      public static string Label(this HtmlHelper helper, string target, string text)
      {
           return String.Format("<label for='{0}'>{1}</label>", target, text);

      }
 }

【讨论】:

  • 不仅没有添加 html 属性,而且它不是生成元素的正确方法,也不会创建经过清理的 id 属性。建议您探索source code
猜你喜欢
  • 2010-11-21
  • 2015-08-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多