David C
David C

Reputation: 2796

WCF showing DisplayAttribute or DisplayName from DataMember

How can I get the DisplayName / Name Attribute to display over my WCF Service?

This is the object in the WCF Service.

[DataContract]
public class Person
{
    [DataMember]
    public int PersonId { get; set; }
    [DataMember]
    [Display(Name = "Name")]
    public string Name { get; set; }
    [DataMember]
    [DisplayName("Date of Birth")]
    public DateTime? DateOfBirth { get; set; }
    [DataMember]
    public string Gender { get; set; }
    [DataMember]
    public string Telephone { get; set; }
    [DataMember]
    public string Email { get; set; }
}

I want the descriptive name to show on the form I've created based on the WCF Service. How can I do that?

Display code on MVC4 View

<div id="online">

        @Html.EditorFor(m => m.Name)

        @Html.EditorFor(m => m.Telephone)
        @Html.EditorFor(m => m.Fax)
        @Html.EditorFor(m => m.Email)

</div>

Upvotes: 4

Views: 2731

Answers (1)

CodeCaster
CodeCaster

Reputation: 151588

I want the descriptive name to show on the form I've created based on the WCF Service. How can I do that?

You can't.

The attributes are read by the serializer, but they are defined in your service assembly. What gets sent to the client depends on the binding used, but in for example a SOAP message (*HttpBinding) you will need the [DataMember(Name = "Foo")] attribute, because that causes the serializer to alter the name of the element in the XML.

You'll have to apply the appropriate attribute to the class in the client proxy.

The alternative would be to tick Reuse Types when generating the reference; then your client will use the classes from the same assembly the service uses, and so the attributes will be accessible in the client.

Upvotes: 6

Related Questions