【问题标题】:How can I override the @Html.LabelFor template?如何覆盖 @Html.LabelFor 模板?
【发布时间】:2011-07-08 22:51:27
【问题描述】:

我有一个简单的字段表单

<div class="field fade-label">
    @Html.LabelFor(model => model.Register.UserName)
    @Html.TextBoxFor(model => model.Register.UserName)
</div>

这会导致:

<div class="field fade-label">
    <label for="Register_UserName">Username (used to identify all services, from 4 to 30 chars)</label>
    <input type="text" value="" name="Register.UserName" id="Register_UserName">
</div>

但我希望LabelFor 代码在里面附加一个&lt;span&gt;,这样我最终可以拥有:

<label for="Register_UserName">
    <span>Username (used to identify all services, from 4 to 30 chars)</span>
</label>

我该怎么做?

所有examples 都使用EditorTemplates,但这是LabelFor

【问题讨论】:

  • 这将导致模棱两可的调用异常,因为签名与现有的扩展方法相同。没有覆盖的扩展方法。
  • @Nilzor,没有带有此类参数的扩展名,您可以安全地使用我的答案中的代码,记住,它是 LabelFor 而不是 EditorFor
  • 是的,你是对的。我应该说的是您的方法不会覆盖 @Html.LabelFor(model => model.Register.UserName) 构造。如果您尝试使用此签名添加重载,您将得到一个模棱两可的调用异常,正如我测试过的那样。您的解决方案是合理的,但它需要您更改调用代码(视图)。
  • @balexandre 你如何覆盖“正常”的 LabelFor 方法?
  • 请将您编辑的解决方案移到下面的实际答案中。

标签: asp.net-mvc templates asp.net-mvc-3


【解决方案1】:

您可以通过创建自己的 HTML 帮助程序来做到这一点。

http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

您可以通过下载 ASP.Net MVC 的源代码来查看 LabelFor 的代码,并将其修改为自定义帮助程序。


答案balexandre

添加
public static class LabelExtensions
{
    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, IDictionary<string, object> htmlAttributes)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        if (String.IsNullOrEmpty(labelText))
        {
            return MvcHtmlString.Empty;
        }

        TagBuilder tag = new TagBuilder("label");
        tag.MergeAttributes(htmlAttributes);
        tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));

        TagBuilder span = new TagBuilder("span");
        span.SetInnerText(labelText);

        // assign <span> to <label> inner html
        tag.InnerHtml = span.ToString(TagRenderMode.Normal);

        return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
    }
}

【讨论】:

  • 知道了,将最终代码添加到我的问题中,供任何人复制/粘贴/使用。感谢您的提醒。
  • 我使用这个扩展方法作为开始,但由于它没有用参数 fieldName 覆盖 LabelFor 方法,所以我对其进行了一些更新。
  • 使用这个时如何解决 Ambigious 调用错误。现在有两个 LabelFor 方法,一个来自默认 MVC 和 this。
  • @Ruchan 你解决过这个问题吗?
  • 不得不用不同的名字重命名我自己的助手。
【解决方案2】:

我扩展了 balalelexandre 的答案,并添加了指定 HTML 以在标签文本之前和之后包含的功能。我添加了一堆方法重载和 cmets。希望对大家有所帮助!

还从这里获取信息:Html inside label using Html helper

namespace System.Web.Mvc.Html
{
    public static class LabelExtensions
    {
        /// <summary>Creates a Label with custom Html before the label text.  Only starting Html is provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml)
        {
            return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}"));
        }

        /// <summary>Creates a Label with custom Html before the label text.  Starting Html and a single Html attribute is provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <param name="htmlAttributes">A single Html attribute to include.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, object htmlAttributes)
        {
            return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAttributes));
        }

        /// <summary>Creates a Label with custom Html before the label text.  Starting Html and a collection of Html attributes are provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <param name="htmlAttributes">A collection of Html attributes to include.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, IDictionary<string, object> htmlAttributes)
        {
            return LabelFor(html, expression, startHtml, null, htmlAttributes);
        }

        /// <summary>Creates a Label with custom Html before and after the label text.  Starting Html and ending Html are provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <param name="endHtml">Html to follow the label text.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml)
        {
            return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}"));
        }

        /// <summary>Creates a Label with custom Html before and after the label text.  Starting Html, ending Html, and a single Html attribute are provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <param name="endHtml">Html to follow the label text.</param>
        /// <param name="htmlAttributes">A single Html attribute to include.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, object htmlAttributes)
        {
            return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAttributes));
        }

        /// <summary>Creates a Label with custom Html before and after the label text.  Starting Html, ending Html, and a collection of Html attributes are provided.</summary>
        /// <param name="startHtml">Html to preempt the label text.</param>
        /// <param name="endHtml">Html to follow the label text.</param>
        /// <param name="htmlAttributes">A collection of Html attributes to include.</param>
        /// <returns>MVC Html for the Label</returns>
        public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, IDictionary<string, object> htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);

            //Use the DisplayName or PropertyName for the metadata if available.  Otherwise default to the htmlFieldName provided by the user.
            string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            if (String.IsNullOrEmpty(labelText))
            {
                return MvcHtmlString.Empty;
            }

            //Create the new label.
            TagBuilder tag = new TagBuilder("label");

            //Add the specified Html attributes
            tag.MergeAttributes(htmlAttributes);

            //Specify what property the label is tied to.
            tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));

            //Run through the various iterations of null starting or ending Html text.
            if (startHtml == null && endHtml == null) tag.InnerHtml = labelText;
            else if (startHtml != null && endHtml == null) tag.InnerHtml = string.Format("{0}{1}", startHtml(null).ToHtmlString(), labelText);
            else if (startHtml == null && endHtml != null) tag.InnerHtml = string.Format("{0}{1}", labelText, endHtml(null).ToHtmlString());
            else tag.InnerHtml = string.Format("{0}{1}{2}", startHtml(null).ToHtmlString(), labelText, endHtml(null).ToHtmlString());

            return MvcHtmlString.Create(tag.ToString());
        }
    }
}

【讨论】:

    【解决方案3】:

    LabelFor 是一种扩展方法(静态),因此不能被覆盖。您需要创建自己的 Html Helper Extension 方法来实现您的要求。

    【讨论】:

    • 编辑器模板可以被“覆盖”,尽管它们也是静态方法。
    • 我们不是在讨论编辑器模板。编辑器模板是通过约定发现的局部视图。它与扩展方法的覆盖或静态声明无关。
    • 是的,但是当有人看到 LabelFor,然后看到它与 EditorFor 相似时,他可能会认为它也可以按照惯例被覆盖。这正是OP所要求的。这与方法重载和静态方法无关。
    猜你喜欢
    • 2023-01-26
    • 2014-05-22
    • 2017-09-03
    • 1970-01-01
    • 2018-12-16
    • 2018-02-20
    • 1970-01-01
    • 2018-06-02
    • 2016-05-18
    相关资源
    最近更新 更多