如何覆盖 @Html.LabelFor 模板? [英] How can I override the @Html.LabelFor template?
问题描述
我有一个简单的字段表单
@Html.LabelFor(model => model.Register.UserName)@Html.TextBoxFor(model => model.Register.UserName)
这导致:
<label for="Register_UserName">用户名(用于标识所有服务,从 4 到 30 个字符)</label><input type="text" value="" name="Register.UserName" id="Register_UserName">
但我希望 LabelFor
代码在里面附加一个 <span>
这样我就可以得到:
我该怎么做?
所有示例都使用EditorTemplates
但这是一个 LabelFor
.
您可以通过创建自己的 HTML 帮助程序来做到这一点.
http://www.asp.net/mvc/教程/creating-custom-html-helpers-cs
您可以通过下载 ASP.Net MVC 的源代码并将其修改为自定义帮助程序来查看 LabelFor<> 的代码.
<小时>答案由 balexandre
添加公共静态类LabelExtensions{public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, object htmlAttributes){返回 LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));}public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, IDictionary htmlAttributes){ModelMetadata 元数据 = ModelMetadata.FromLambdaExpression(expression, html.ViewData);string htmlFieldName = ExpressionHelper.GetExpressionText(expression);字符串 labelText = metadata.DisplayName ??metadata.PropertyName ??htmlFieldName.Split('.').Last();如果 (String.IsNullOrEmpty(labelText)){返回 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);//分配 <span><标签>内部htmltag.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屋!