本文介绍了ASP.NET Core MetaDataType属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在域模型类上使用MetaDataType属性.它应该将属性信息从引用的类移动到已设置MetadataType属性的类中.但这并不像广告中所说的那样.是什么引起这里的问题?
I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set.But it doesn't do as advertised. What is causing the issue here?
[MetadataType(typeof(ComponentModelMetaData))]
public partial class Component
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Repo> Repos { get; set; }
public string Description { get; set; }
}
public class ComponentModelMetaData
{
[Required(ErrorMessage = "Name is required.")]
[StringLength(30, MinimumLength = 3, ErrorMessage = "Name length should be more than 3 symbols.")]
public string Name { get; set; }
public ICollection<Repo> Repos { get; set; }
[Required(ErrorMessage = "Description is required.")]
public string Description { get; set; }
}
推荐答案
ASP.NET Core使用
ASP.NET Core uses
Microsoft.AspNetCore.Mvc **ModelMetadataType**
代替
System.ComponentModel.DataAnnotations.**MetadataType**
尝试将属性更改为[ModelMetadataType(typeof(ComponentModelMetaData))]
这篇关于ASP.NET Core MetaDataType属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!