我怎么能覆盖@ Html.LabelFor模板? [英] How can I override the @Html.LabelFor template?

查看:98
本文介绍了我怎么能覆盖@ Html.LabelFor模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的现场形式

 < D​​IV CLASS =字段褪色标签>
    @ Html.LabelFor(型号=> model.Register.UserName)
    @ Html.TextBoxFor(型号=> model.Register.UserName)
< / DIV>

和这导致:

 < D​​IV CLASS =字段褪色标签>
    <标签=Register_UserName>的用户名(用于识别所有的服务,从4到30个字符)LT; /标签>
    <输入类型=文本值=NAME =Register.UserNameID =Register_UserName>
< / DIV>

但我想那 LabelFor code追加<跨度> 里面,所以我可以结束了有:

 <标签=Register_UserName>
    &所述;跨度>用户名(用于识别的所有服务,从4至30个字符)LT /跨度>
< /标签>

我怎样才能做到这一点?

使用 EditorTemplates 但是这是一个 LabelFor


解决方案

您会通过创建自己的HTML帮助做到这一点。

<一个href=\"http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs\">http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

通过下载ASP.Net MVC源>和修改,作为一个自定义的帮手

您可以查看code为LabelFor&LT。


按添加 balexandre

 公共静态类LabelExtensions
{
    公共静态MvcHtmlString LabelFor&LT;的TModel,TValue&GT;(此的HtmlHelper&LT;的TModel&GT; HTML,防爆pression&LT; Func键&LT;的TModel,TValue&GT;&GT;前pression,对象htmlAttributes)
    {
        返回LabelFor(HTML,前pression,新RouteValueDictionary(htmlAttributes));
    }
    公共静态MvcHtmlString LabelFor&LT;的TModel,TValue&GT;(此的HtmlHelper&LT;的TModel&GT; HTML,防爆pression&LT; Func键&LT;的TModel,TValue&GT;&GT;前pression,IDictionary的&LT;字符串对象&gt; htmlAttributes)
    {
        ModelMetadata元= ModelMetadata.FromLambdaEx pression(如pression,html.ViewData);
        字符串htmlFieldName =前pressionHelper.GetEx pressionText(如pression);
        字符串LabelText的= metadata.DisplayName? metadata.PropertyName? htmlFieldName.Split('。')最后()。
        如果(String.IsNullOrEmpty(LabelText的))
        {
            返回MvcHtmlString.Empty;
        }        TagBuilder标签=新TagBuilder(标签);
        tag.MergeAttributes(htmlAttributes);
        tag.Attributes.Add(代表,html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));        TagBuilder跨度=新TagBuilder(跨度);
        span.SetInnerText(LabelText的);        //分配&LT;跨度&GT;到&lt;标签&gt;内部HTML
        tag.InnerHtml = span.ToString(TagRenderMode.Normal);        返回MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
    }
}

I have a simple field form

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

and this results in:

<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>

but I want that LabelFor code append a <span> inside so I can end up having:

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

How can I do this?

All examples use EditorTemplates but this is a LabelFor.

解决方案

You'd do this by creating your own HTML helper.

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

You can view the code for LabelFor<> by downloading the source for ASP.Net MVC and modify that as a custom helper.


Answer added by 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));
    }
}

这篇关于我怎么能覆盖@ Html.LabelFor模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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