Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add MetadataType to derived class in C#

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

like image 735
Vinay Avatar asked Oct 10 '22 04:10

Vinay


1 Answers

It was the problem in MVC2. However it works in MVC3. See the following article :

http://connect.microsoft.com/VisualStudio/feedback/details/538360/asp-net-mvc-2-rc2-the-client-side-validation-does-not-work-with-overridden-properties

like image 95
Mansoor Gee Avatar answered Oct 18 '22 01:10

Mansoor Gee