简体   繁体   English

DataTypeAnnotations MetadataType 未按预期工作

[英]DataTypeAnnotations MetadataType not working as expected

I try to implement Metadatatype, in order to seperate Validation attributes from my Acquisitiecode class, into the AcquisitiecodeAnnotations class.我尝试实现元数据类型,以便将验证属性从我的 Acquisitiecode class 中分离到 AcquisitiecodeAnnotations class 中。

Now when I add attributes (like Required, StringLength and so on) to the Acquisitiecode class, validation works as expected.现在,当我向 Acquisitiecode class 添加属性(如Required、StringLength 等)时,验证按预期工作。 When I move these attributes to the AcquisitiecodeAnnotations class and bind this class using the MetadataType attribute, I does not work.当我将这些属性移动到 AcquisitiecodeAnnotations class 并使用 MetadataType 属性绑定此 class 时,我不起作用。

Please find the code examples below (I've stripped them down for readability).请在下面找到代码示例(为了便于阅读,我已将它们剥离)。 Also, the project is an ASP.NET Core 3.0 web application.此外,该项目是 ASP.NET Core 3.0 web 应用程序。 All code, including the examples are also running in.NET Core 3.0 projects.所有代码(包括示例)也在 .NET Core 3.0 项目中运行。

Snippet 1:片段1:

using System;
using System.ComponentModel.DataAnnotations;

namespace Shared.Entities
{
    [MetadataType(typeof(AcquisitiecodeAnnotations))]
    public partial class Acquisitiecode
    { }

    public partial class AcquisitiecodeAnnotations
    {
        [StringLength(4, ErrorMessage = "The value cannot exceed 4 characters. ")]
        public string Acquisitiecode1 { get; set; }
    }
}

Snippet 2:片段 2:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Shared.Entities
{
    public partial class Acquisitiecode
    {
        public Acquisitiecode()
        {
            Lidmaatschap = new HashSet<Lidmaatschap>();
        }

        public string Acquisitiecode1 { get; set; }

        public virtual Lid Lid { get; set; }
        public virtual ICollection<Lidmaatschap> Lidmaatschap { get; set; }
    }
}

As of October 2020 the current version of Blazor does not support Metadatatype .截至 2020 年 10 月,当前版本的Blazor不支持Metadatatype For more information please read this issue .欲了解更多信息,请阅读本期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM