Html.Label
和Html.LabelFor
帮助程序方法不像大多数其他帮助程序那样支持htmlAttributes参数。但是我想设置class
。事情是这样的:如何扩展MVC3标签和LabelFor HTML助手?
Html.LabelFor(model => model.Name, new { @class = "control-label" })
是否有扩展标签/ LabelFor 没有诉诸复制和扩大这些方法的ILSpy DISASM输出的容易方式?
Html.Label
和Html.LabelFor
帮助程序方法不像大多数其他帮助程序那样支持htmlAttributes参数。但是我想设置class
。事情是这样的:如何扩展MVC3标签和LabelFor HTML助手?
Html.LabelFor(model => model.Name, new { @class = "control-label" })
是否有扩展标签/ LabelFor 没有诉诸复制和扩大这些方法的ILSpy DISASM输出的容易方式?
,您可以轻松创建自己的LabelFor延长标签:
像这样的东西应该做的,你需要
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));
tag.SetInnerText(labelText);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
更新 在使用时只需在你的项目中创建的扩展方法是什么,将此行加入您的Views\web.config
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="MyProject.Helpers" /> <-- my extension
...
</namespaces>
</pages>
public static class LabelExtensions
{
public static IHtmlString LabelFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
string labelText,
object htmlAttributes
)
{
var metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
return LabelHelper(htmlHelper, metadata, htmlFieldName, labelText, htmlAttributes);
}
public static IHtmlString Label(this HtmlHelper htmlHelper, string expression, string labelText, object htmlAttributes)
{
var metadata = ModelMetadata.FromStringExpression(expression, htmlHelper.ViewData);
return LabelHelper(htmlHelper, metadata, expression, labelText, htmlAttributes);
}
private static IHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText, object htmlAttributes)
{
string str = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>()));
if (string.IsNullOrEmpty(str))
{
return MvcHtmlString.Empty;
}
TagBuilder tagBuilder = new TagBuilder("label");
tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
var attributes = new RouteValueDictionary(htmlAttributes);
tagBuilder.MergeAttributes(attributes);
tagBuilder.SetInnerText(str);
return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}
}
然后:
@Html.LabelFor(x => x.SomeProperty, null, new { @class = "foo" })
或:
@Html.Label("SomeProperty", null, new { @class = "foo" })
对不起,如果我没有让自己清楚。我已经完成了这个(复制MVC源代码并将其扩展),但是由于多种原因,我想避免这种情况。我也喜欢让事情干燥。我希望能有更优雅的东西。 – batkuip 2012-03-27 08:43:39
对不起,没有什么可以让你做到这一点。内置的帮助程序不可扩展,并且具有硬编码的属性。 – 2012-03-27 09:03:13
如何让编译器在myProject.Helpers.LabelExtensions.LabelFor和'System.Web.Mvc.Html.LabelExtensions.LabelFor(页面上的其他东西需要)之间消除歧义? – Ungaro 2013-12-20 03:42:22
我知道,但我想避免复制拆卸/原始源输出。原始代码由22个方法组成,正确执行此操作,我将不得不重写它们。 – batkuip 2012-03-27 08:19:12
没有。您只需创建您的扩展项目并在您的web.config中插入对您的项目的调用。 – Iridio 2012-03-27 08:23:57