【发布时间】:2015-12-19 22:42:17
【问题描述】:
我有一个对象:具有多个属性(品牌、型号和颜色)的汽车。每个属性都有一个分配的属性 (BilingualLabel)。
我正在尝试为分配有 BilingualLabel 属性的任何类型编写强类型 htmlhelper。 IE。我想获取我归因于 Car 类中的属性的“MakeFR”文本。
例如
[System.AttributeUsage(AttributeTargets.Property)]
public class BilingualLabel : System.Attribute
{
private string _resourceCode = "";
public BilingualLabel(string ResourceCode)
{
_resourceCode = ResourceCode;
}
}
然后是汽车类:
public class Car
{
[BilingualLabel("MakeFR")]
public string Make { get; set; }
[BilingualLabel("ModelFR")]
public string Model { get; set; }
[BilingualLabel("ColorFR")]
public string Color { get; set; }
}
现在我来到帮助类,我似乎无法获得我设置的属性值。我已经展示了我尝试过的两种尝试。它们都作为空属性返回。
public static MvcHtmlString BilingualLabelFor<T,E>(this HtmlHelper<T> htmlHelper, Expression<Func<T,E>> expression)
{
//get the Bilingual Label resource code
//BilingualLabel attr =
// (BilingualLabel)Attribute.GetCustomAttribute(typeof(E), typeof(BilingualLabel));
MemberExpression member = expression.Body as MemberExpression;
PropertyInfo propInfo = member.Member as PropertyInfo;
MvcHtmlString html = default(MvcHtmlString);
return html;
}
【问题讨论】:
-
您的资源是静态的吗?可以使用localization 的内置功能吗?
标签: asp.net-mvc model-view-controller html-helper custom-attributes