2011-08-22 15 views
5

私はと呼ばれるクラスを持っているは、C#で派生クラスにたmetadataTypeを追加

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

私はPersonMetaDataと呼ばれる

私は人と呼ばれる私の第三のクラスが宣言されている
public partial class Person : Contact 
{ 
    public string Occupation { get; set; } 
    public string Country { get; set; } 
} 

部分と第4のクラスと呼ばれる別のクラスを持っています私のMVCビューで注釈

[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; } 
    } 

を宣言するために使用され、私は強く型付けをしましたPersonに基づくページ?問題は、必須であり、文字列長の検証が機能しないことです。この問題は、クラスを継承し、MetadataTypeを参照する部分クラスを作成するときに発生します。

継承がない場合、部分クラスを呼び出すときにMetadataTypeが正常に動作します。

任意派生クラスにMetadataTypeを使用し、partialを使用しているときのすべてのソリューションはありますか?

ありがとうございました

+0

はまだ答えを受け入れませんでした。 –

答えて

1

は、その部分を削除し、これを試してみてください。

[MetadataType(typeof(PersonMetadata))] 
public partial class Person : Contact 
{ 
    public string Occupation { get; set; } 
    public string Country { get; set; } 
} 
関連する問題