0

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.

gdoron
  • 147,333
  • 58
  • 291
  • 367
Smithy
  • 2,170
  • 6
  • 29
  • 61

2 Answers2

1

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 :)

Smithy
  • 2,170
  • 6
  • 29
  • 61
0

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

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • ah, ok. So is it possible to apply MVC data annotations to a WCF datacontract object? I need them to be applied in my MVC solution because my localisation is in the MVC project too. – Smithy Nov 12 '12 at 15:56
  • @Smithy. If you're using in the WCF project the class defined in the MVC DLL all the attributes applied there will apply in the WCF as well. – gdoron Nov 12 '12 at 15:59
  • I know but I'm using localisation in my MVC project I need to be able to apply MyResources.strings.PeopleOnTourCount because my MVC app is in different languages, your solution de-centralises the localisation (on the assumption I also transfer the model localisations to the WCF project) – Smithy Nov 12 '12 at 16:02
  • @Smithy. It's hard (at least for me) to help you more than that from a distance. sorry. – gdoron Nov 12 '12 at 16:04
  • I've put up an interim solution, does it shed any light on my mess of a question? – Smithy Nov 12 '12 at 16:15
  • @Smithy, it's my answer and comments, so... nope. – gdoron Nov 12 '12 at 16:22