首页 新闻 会员 周边

求助,MVC 2 如何将源码引用到项目或者谁帮我实现扩展DescriptionAttribute

0
悬赏园豆:100 [已关闭问题]

MVC1我会引用,但是MVC2试了好久都不行,在这里我B4一下MVC2为什么在ModelMetadata中不实现DescriptionAttribute。修改了源码,但是不知道怎么引用项目。大家帮忙一下。

如果能帮我实现ModelMetadata的DescriptionAttribute更好,谢谢啦。

SVN的主页 SVN | 初学一级 | 园豆:100
提问于:2010-04-03 00:26
< >
分享
其他回答(1)
0

新建类 DescriptionMetadataProvider

然后重写DataAnnotationsModelMetadataProvider的CreateMetadata方法:

public class DescriptionMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
List
<System.Attribute> attributeList = new List<System.Attribute>(attributes);
DataAnnotationsModelMetadata result
= (DataAnnotationsModelMetadata)base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

DescriptionAttribute descriptionAttribute
= attributeList.OfType<DescriptionAttribute>().FirstOrDefault();
if (descriptionAttribute != null)
{
result.Description
= descriptionAttribute.Description;
}
return result;
}
}

 

接下来在Global.asax的Application_Start()中注册DescriptionMetadataProvider

ModelMetadataProviders.Current = new DescriptionMetadataProvider();

编译后,你就可以使用DescriptionAttribute属性了。

Leejor. | 园豆:143 (初学一级) | 2010-04-03 01:49
0

直接用mvc2的路过。

风海迷沙 | 园豆:4453 (老鸟四级) | 2010-04-03 14:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册