【发布时间】:2020-08-08 09:35:39
【问题描述】:
我希望对 HTML Helper 进行扩展,以显示我的 ViewModel 属性的描述。这是列表,因为自从How do I display the DisplayAttribute.Description attribute value? 以来,ASP.NET Core 3.1 中的事情发生了变化。
这是我的扩展方法:
public static string DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression) {
MemberExpression memberExpression = (MemberExpression)expression.Body;
var displayAttribute = (DisplayAttribute)memberExpression.Member.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
string description = displayAttribute?.Description ?? memberExpression.Member?.Name;
//But how can i localize this description
return description;
}
但现在我需要对其进行本地化,例如
@Html.DisplayNameFor(model => model.MyNotObviousProperty)
如何在我的扩展方法中检索 DataAnnotationLocalizer?当然,我可以像参数一样传递它,但是当DisplayNameFor 不要求额外的参数时,它不是很好的解决方案。
【问题讨论】:
标签: c# localization asp.net-core-mvc data-annotations asp.net-core-3.1