简体   繁体   中英

reference wcf class in mvc for data annotation validation

I have a data contract class with data members in my WCF project and I want to reference them in my MVC project so I can apply data annotation validation to them

I can use the class object in my MVC project already the only problem is the validation.

In my WCF project my class has a property called PeopleOnTourCount :

    namespace VBSClient.BookingServiceClient 
    {
        [DataContract]
        [MetadataType(typeof(BookingTypeMetaData))]
        public partial class BookingType 
        {
            public BookingType() { }
        }

        public class BookingTypeMetaData {
            [Required]
            [Display(Name="People Count")]
            [DataMember]
            public int PeopleOnTourCount { get; set; }
        }
    }

I can't access any of my original properties inside the constructor and the annotations aren't binding either.

Instead of using partial class, inherit from the object instead.

You can then apply your data annotations in the MVC project.

[MetadataType(typeof(BookingTypeMetaData))]
public class Test : BookingType {

    public Test() {

    }
}

public class BookingTypeMetaData {
    [Required]
    [Display(Name = "People Count")]
    public int PeopleOnTourCount { get; set; }
}

This is how I'm going to deal with it unless a better answer is given :)

You can't bind two Partial classes from two separate Assembly to one class.
Partial classes should be in one assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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