用于包装HTML标记的LabelFor的帮助器 [英] Helper for a LabelFor which wraps an HTML tag

查看:109
本文介绍了用于包装HTML标记的LabelFor的帮助器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些具有长名称的实体,我希望能够使用HTML abbr标签在标签中缩写它们.我利用DataAnnotations为视图模型(或实体类)中的属性提供DisplayName值.

I have some entities with long names and I'd like to be able to abbreviate them in labels, using the HTML abbr tag. I make use of DataAnnotations to provide a DisplayName value for the property in the view model (or the entity class).

我一直在手动执行以下操作:

I've been manually doing this like:

<label class="myClass"><abbr title="@Html.DisplayFor(m => m.Ssn)">SSN</abbr> @Html.EditorFor(m => m.Ssn)</label>

但是它正在变成一种痛苦.我想用以下语法设计一个助​​手.

but it's becoming a pain. I'd like to design a helper with the following syntax.

@Html.AbbrLabelFor(m => m.Ssn, "SSN")

并输出:

<label for="Ssn"><abbr title="Social Security Number">SSN</abbr></label>

其中社会安全号码"是DisplayName值,帮助程序标记中的第二个参数是缩写文本.因此,基本上只需将第二个参数添加到混合中即可.

where "Social Security Number" is the DisplayName value, and the second argument in the helper tag is the abbreviation text. So basically just adding the second argument to the mix.

我所看到的示例倾向于放弃HtmlAttributes之类,而我想保留该功能,如:

The examples I've seen tend to ditch the HtmlAttributes and such, and I'd like to retain that functionality, as in:

@Html.AbbrLabelFor(m => m.Ssn, "SSN", new { @class="myClass" })

我如何扩展现有的功能齐全的LabelFor来实现此功能(例如DfnLabel等)?

How do I extend the existing, full-featured LabelFor to implement this, a DfnLabel, etc.?

推荐答案

您的助手需要看起来像

public static MvcHtmlString AbbrLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string abbreviation, object htmlAttributes)    
{
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    string name = ExpressionHelper.GetExpressionText(expression)       
    TagBuilder abbr = new TagBuilder("abbr");
    abbr.MergeAttribute("title", metaData.GetDisplayName());
    abbr.InnerHtml = abbreviation;
    TagBuilder label = new TagBuilder("label");
    label.MergeAttribute("for", name);
    label.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    label.InnerHtml = abbr.ToString();

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

编辑(带有重载)

public static MvcHtmlString AbbrLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string abbreviation)
{
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    string name = ExpressionHelper.GetExpressionText(expression);
    return AbbrLabelHelper(helper, metaData, name, abbreviation, null);
}

public static MvcHtmlString AbbrLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string abbreviation, object htmlAttributes)
{
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    string name = ExpressionHelper.GetExpressionText(expression);
    return AbbrLabelHelper(helper, metaData, name, abbreviation, htmlAttributes);
}

private static MvcHtmlString AbbrLabelHelper(HtmlHelper helper, ModelMetadata metaData, string name, string abbreviation, object htmlAttributes)
{
    TagBuilder abbr = new TagBuilder("abbr");
    abbr.MergeAttribute("title", metaData.GetDisplayName());
    abbr.InnerHtml = abbreviation;
    TagBuilder label = new TagBuilder("label");
    label.MergeAttribute("for", name);
    label.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    label.InnerHtml = abbr.ToString();
    return MvcHtmlString.Create(label.ToString());
}

这篇关于用于包装HTML标记的LabelFor的帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆