无法获取自定义属性值在HTML MVC3助手 [英] Can't get Custom Attribute Value in MVC3 HTML Helper
问题描述
我扩展HTML辅助与需要从模型的属性的属性值的方法。所以,我已经定义了一个自定义属性本身。
公共类ChangeLogFieldAttribute:属性{
公共字符串字段名{获得;组; }
}
它的使用方法是在我的模型。
[显示(NAME =风格)]
[ChangeLogField(字段名=styleid)]
公共字符串样式{搞定;组; }
在我的辅助方法,我有以下code,让我的属性的字段名的值,如果使用该属性的属性。
VAR ITEMNAME =((MemberEx pression)ex.Body).Member.Name; VAR containerType = html.ViewData.ModelMetadata.ContainerType;
var属性= ((ChangeLogFieldAttribute[])containerType.GetProperty(html.ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(ChangeLogFieldAttribute), FALSE))FirstOrDefault()。
如果(属性!= NULL){
ITEMNAME = attribute.FieldName;
}
然而,当我达到这个code,我得到一个异常,因为containerType为null。
我不知道如果我做任何这是正确的,但我从大约4个不同来源扯远得到这个。如果你能提出一个修正我的问题或替代方案,我会很感激。
感谢。
与Solution更新
我用达林季米特洛夫的解决方案,尽管我不得不调整它的一些。下面是我加的。我不得不检查属性metatdata的存在,一切都很好。
VAR字段名=((MemberEx pression)ex.Body).Member.Name; VAR元= ModelMetadata.FromLambdaEx pression(例如,html.ViewData);
如果(metadata.AdditionalValues.ContainsKey(fieldName的)){
字段名=(字符串)metadata.AdditionalValues [字段名];
}
您可以使属性的元数据清楚:
公共类ChangeLogFieldAttribute:属性,IMetadataAware
{
公共字符串字段名{获得;组; } 公共无效OnMetadataCreated(ModelMetadata元数据)
{
metadata.AdditionalValues [字段名] =字段名;
}
}
,然后助手里面的:
VAR元= ModelMetadata.FromLambdaEx pression(例如,htmlHelper.ViewData);
VAR字段名= metadata.AdditionalValues [字段名];
I've extended the HTML helper with a method that needs an attribute value from the property of the model. So I've defined a custom attribute as such.
public class ChangeLogFieldAttribute : Attribute {
public string FieldName { get; set; }
}
It's used like this in my model.
[Display(Name = "Style")]
[ChangeLogField(FieldName = "styleid")]
public string Style { get; set; }
In my helper method, I've got the following code to get the FieldName value of my attribute, if the attribute is used for the property.
var itemName = ((MemberExpression)ex.Body).Member.Name;
var containerType = html.ViewData.ModelMetadata.ContainerType;
var attribute = ((ChangeLogFieldAttribute[])containerType.GetProperty(html.ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(ChangeLogFieldAttribute), false)).FirstOrDefault();
if (attribute != null) {
itemName = attribute.FieldName;
}
However, when I reach this code, I get an exception because the containerType is null.
I'm not sure if I'm doing any of this correct, but I pulled from about 4 different sources to get this far. If you could suggest a fix to my problem or an alternative, I'd be grateful.
Thanks.
UPDATE WITH SOLUTION
I used Darin Dimitrov's solution, although I had to tweak it some. Here is what I added. I had to check for the existence of the attribute metatdata and all was good.
var fieldName = ((MemberExpression)ex.Body).Member.Name;
var metadata = ModelMetadata.FromLambdaExpression(ex, html.ViewData);
if (metadata.AdditionalValues.ContainsKey("fieldName")) {
fieldName = (string)metadata.AdditionalValues["fieldName"];
}
You could make the attribute metadata aware:
public class ChangeLogFieldAttribute : Attribute, IMetadataAware
{
public string FieldName { get; set; }
public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.AdditionalValues["fieldName"] = FieldName;
}
}
and then inside the helper:
var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData);
var fieldName = metadata.AdditionalValues["fieldName"];
这篇关于无法获取自定义属性值在HTML MVC3助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!