0
我希望能够以编程方式覆盖属性的显示名称。覆盖视图模型属性名称
我有一个视图模型,其中显示名称已定义。
[Display(Name = "Reference")]
public string Reference { get; set; }
我为带有字符串重写参数的LabelFor()扩展创建了一个重载。
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string applicationVariableOverride)
{
if (applicationVariableOverride != null)
return new MvcHtmlString(applicationVariableOverride);
else
return System.Web.Mvc.Html.LabelExtensions.LabelFor<TModel, TValue>(htmlHelper, expression);
}
...调用它是这样的:
@Html.LabelFor(x => x.Reference, "New Reference Display Name")
这个伟大的工程的标签,但不适用于作为文本被设置使用这些属性验证消息的工作是动态的,根据什么样的信息正在显示。相反,我想传递(或设置)此功能中的属性的显示名称。
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string applicationVariableOverride)
{
// set display name
return System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor<TModel, TProperty>(htmlHelper, expression);
}
难道我直接访问属性,并尝试和它的属性(显示名称)的访问的一个或有另一种更简单的方法来做到这一点?
谢谢
感谢您的回复。我想重写该属性,因为这将是设置显示名称的逻辑/正确位置,但它不会让我传递任何不是常量的东西...... – Beakie