Client Validations through Data Annotations and WCF Services in ASP.NET MVC Application


Having trouble making the client validations applied through Data Annotations work in an ASP.NET MVC application when the model is provivded from a WCF service?? This article explains how to overcome this issue.

Introduction 
Data Annotations attributes are not serialized automatically by the WCF services. The Data Contracts when sent over WCF Services, the Data Annotation attributes are removed. Hence all the client validations built on top of this data contract does not work on your MVC view page. There are few alternatives that can be tried to make this work.


1. Referencing Data Contracts library to your Web App 
To make the validations that are applied in your model class work when the model is sent over the WCF services

 (a) Extract all the Data Contract classes (Model classes)
 (b) Group them under a separate library.
 (c) Add the reference of this library to your web application.
 (d) While adding the service reference of your WCF Service, go to Advanced tab and make sure you check the option - "Reuse types in Referenced Assemblies" and select "Reuse types in All Referenced assemnlies".
 (e)  By selecting this option, the WCF services makes use of the Data Contract reference that you have already added instead of adding a new WCF Assembly reference.
 (f) Now try to use the model that you have referenced in your View from the assembly that is referenced.

 

 Thats it, it will make all your validations work as usual.

2. Extend your Model Class in the Web
After Adding the Service reference to your web, open the Reference.cs file added to your Web application. Copy the name space of the Reference.cs file and create a new class with the same name of your model class to which you would like to have the validations work with the same name space. Make sure that you have your model class declared as partial in your Model library.

     namespace SampleApp.ServiceReference1 {     using System.Runtime.Serialization;     using System;
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization""4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name = "Class1", Namespace = "http://schemas.datacontract.org/2004/07/SampleApp.Model")]
    [System.SerializableAttribute()]
    public partial class Class1 : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
    { } }
 In the above code, Class1 is the Model Class that is being passed from the Model library to Web through WCF services. Add the partial class with the same name. Refer to the below code for sample. Notice that the name space is same as that of the WCF auto-generated code.

namespace SampleApp.ServiceReference1 {
    using System.Runtime.Serialization;
    using System;


    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization""4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name = "Class1", Namespace = "http://schemas.datacontract.org/2004/07/SampleApp.Model")]
    [System.SerializableAttribute()]
    public partial class Class1 : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
    { } }

Now, add a new class with the same name as your model class but suffixed with Validations text to it.  
public class Class1Validations  {
        [Required]
        [Display(Name = "Customer Name")]
        public string Name { getset; }
    }
 }
This class applies a required field validation on the Name field. Class1 is the model class and its validation class is Class1Validations. 

Now its time to link the Class1 class and Class1Validations to make the validations defined work. To make this work, we need to add one line on top of the extended partial class (Class1). Now the extended partial class looks like below.

namespace SampleApp.ServiceReference1{
    [MetadataType(typeof(Class1Validations))]
    public partial class Class1
    {
    }
}

Thats all... We are ready to use the Model class and the validations work normal.

Conclusion 
This articles explains the possible ways of making validations applied through Data Annotations on an ASP.NET MVC 3 application.

Related tags

ASP.NET MVC3, Data Annotations