ASP.NET Core MetaDataType 属性不起作用 [英] ASP.NET Core MetaDataType Attribute not working

查看:41
本文介绍了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))]

Try changing your attribute to [ModelMetadataType(typeof(ComponentModelMetaData))]

这篇关于ASP.NET Core MetaDataType 属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆