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.

4
  • The question is too vague or is it just me? Commented Nov 12, 2012 at 15:48
  • what is vague about it? I have a partial class in my WCF project, a partial class in my MVC app and I'm trying to apply data annotations. It's not working. Commented Nov 12, 2012 at 15:51
  • Now I got your question, it can't be done in two different assemblies. read my answer below. Commented Nov 12, 2012 at 15:54
  • Thanks for the reply :) read my comment below your answer below hehe. Commented Nov 12, 2012 at 15:58

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

6 Comments

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. 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.
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. It's hard (at least for me) to help you more than that from a distance. sorry.
I've put up an interim solution, does it shed any light on my mess of a question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.