我已经在我的属性上添加了一个Description属性,但是ModelMetada上的Description属性仍然是null。
[Description("sss")]
public int Id { get; set; }顺便说一句,I've putted是怎么回事?
编辑
我已经看过MVC源代码了。好像不是虫子。从不使用decsription属性。元数据类中有一个属性,但从未设置或调用该属性。CreateMetadata方法没有代码可以使用,attribute.The解决方案将覆盖create方法并编辑模板。
发布于 2010-07-08 19:11:09
在试图找到如何实现这一目标时,我遇到了一篇博客文章,其中说描述和水印对于当前的DataAnnotations框架都是不可用的。
我想出了一个大致是这样的解决办法:
(免责声明:这段代码是从我的编译版本中编辑出来的,目的是从通过组合构建的元数据提供程序中删除它,这样它就不能直接编译,而不需要进行一些触摸。)
public class CustomDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, System.Type containerType, System.Func<object> modelAccessor, System.Type modelType, string propertyName)
{
var baseModelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
var result = new CustomMetadata(modelMetadataProvider, containerType, modelAccessor, modelType, propertyName, attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(), attributes)
{
TemplateHint = !string.IsNullOrEmpty(templateName) ? templateName : baseModelMetaData.TemplateHint,
HideSurroundingHtml = baseModelMetaData.HideSurroundingHtml,
DataTypeName = baseModelMetaData.DataTypeName,
IsReadOnly = baseModelMetaData.IsReadOnly,
NullDisplayText = baseModelMetaData.NullDisplayText,
DisplayFormatString = baseModelMetaData.DisplayFormatString,
ConvertEmptyStringToNull = baseModelMetaData.ConvertEmptyStringToNull,
EditFormatString = baseModelMetaData.EditFormatString,
ShowForDisplay = baseModelMetaData.ShowForDisplay,
ShowForEdit = baseModelMetaData.ShowForEdit,
DisplayName = baseModelMetaData.DisplayName
};
return result;
}
}
public class CustomMetadata : DataAnnotationsModelMetadata
{
private string _description;
public CustomMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable<Attribute> attributes)
: base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
{
var descAttr = attributes.OfType<DescriptionAttribute>().SingleOrDefault();
_description = descAttr != null ? descAttr.Description : "";
}
// here's the really important part
public override string Description
{
get
{
return _description;
}
set
{
_description = value;
}
}
}然后,在Global.asax中的Application_Start或任何注册模型元数据提供程序的地方:
ModelMetadataProviders.Current = new CustomMetadataProvider();发布于 2011-06-14 20:14:26
这是一个旧的帖子,但遇到这个也有一个稍微不同的解决方案与cfeduke。我想我会把它发出去以防其他人在这里发生。
至少在MVC 3中,您不需要定义自定义元数据类型,只需要定义提供程序。阅读MVC源代码,元数据并不是从所有可能的属性构建的,只是一些:
DisplayAttribute提供:
根本不检查DescriptionAttribute,因此如果在模型上定义了元数据,元数据将反映为null。如果您在未设置元数据属性方面遇到问题,请检查提供程序是否确实读取该属性。
class MyDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metaData = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
// Description - The default provider reads the description from DisplayAttribute.
// Here we check for a description attribute as well. This will overwrite anything
// set before as we assume a Description attribute is more specific.
DescriptionAttribute descriptionAttribute = attributes.OfType<DescriptionAttribute>().FirstOrDefault();
if (descriptionAttribute != null)
{
metaData.Description = descriptionAttribute.Description;
}
return metaData;
}
}发布于 2010-06-02 01:19:31
正确的属性是DisplayNameAttribute。您可以执行自己的属性,但它必须从DisplayNameAttribute派生。
https://stackoverflow.com/questions/2826083
复制相似问题