在 C# 中将 MetadataType 添加到派生类

发布于 2024-11-30 12:02:53 字数 1431 浏览 0 评论 0原文

我有一个名为的类

public partial class Contact
{
   public int Id { get; set; }
   public string Title { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

我有另一个名为的类

public partial class Person : Contact
{
   public string Occupation { get; set; }
   public string Country { get; set; }
}

我有我的第三个类名为Person,它被声明为Partial,第四个类名为PersonMetaData,用于声明注释

 [MetadataType(typeof(PersonMetadata))]
    public partial class Person : Contact
    {

    }

    public class PersonMetadata
    {
         [StringLength(20, ErrorMessageResourceName = "FirstNameLength",   
         ErrorMessageResourceType = typeof(BasicErrors))]
        [Required(ErrorMessageResourceName = "FirstNameRequired",  
          ErrorMessageResourceType  = typeof(BasicErrors))]
        public string FirstName { get; set; }

        [StringLength(20, ErrorMessageResourceName = "LastNameLength", 
         ErrorMessageResourceType = typeof(BasicErrors))]
        [Required(ErrorMessageResourceName = "LastNameRequired", ErrorMessageResourceType 
         = typeof(BasicErrors))]
        public string LastName { get; set; }
    }

在我的MVC视图中,我制作了一个基于Person的强类型页面?问题是,必需的和字符串长度验证不起作用。当继承类并创建分部类来引用 MetadataType 时,会出现此问题。

如果没有继承,那么 MetadataType 在调用部分类时可以正常工作。

将 MetadataType 用于派生类并与其一起使用部分时有任何解决方案吗?

谢谢

I have a Class called

public partial class Contact
{
   public int Id { get; set; }
   public string Title { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

I have another Class called

public partial class Person : Contact
{
   public string Occupation { get; set; }
   public string Country { get; set; }
}

I have my third class called Person which is declared Partial and Fourth class called PersonMetaData used to declare annotations

 [MetadataType(typeof(PersonMetadata))]
    public partial class Person : Contact
    {

    }

    public class PersonMetadata
    {
         [StringLength(20, ErrorMessageResourceName = "FirstNameLength",   
         ErrorMessageResourceType = typeof(BasicErrors))]
        [Required(ErrorMessageResourceName = "FirstNameRequired",  
          ErrorMessageResourceType  = typeof(BasicErrors))]
        public string FirstName { get; set; }

        [StringLength(20, ErrorMessageResourceName = "LastNameLength", 
         ErrorMessageResourceType = typeof(BasicErrors))]
        [Required(ErrorMessageResourceName = "LastNameRequired", ErrorMessageResourceType 
         = typeof(BasicErrors))]
        public string LastName { get; set; }
    }

In my MVC View, I have made a strongly typed page based on Person? The problem is, required and string length validations do not work. This problem occurs when inheriting a class and creating a partial class to reference the MetadataType.

If there is no inheritance then MetadataType works fine when calling a Partial Class.

Any Solutions when using MetadataType for derived class and using partial with it ??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-12-07 12:02:53

这是MVC2中的问题。然而它在 MVC3 中工作。请参阅以下文章:

恏ㄋ傷疤忘ㄋ疼 2024-12-07 12:02:53

删除该部分并尝试以下操作:

[MetadataType(typeof(PersonMetadata))]
public partial class Person : Contact
{
   public string Occupation { get; set; }
   public string Country { get; set; }
}

Remove that partial and try this:

[MetadataType(typeof(PersonMetadata))]
public partial class Person : Contact
{
   public string Occupation { get; set; }
   public string Country { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文