简体   繁体   中英

WEB Api not sending Enum to client

I have got one class "ApplicationSetting" inherited from "BaseSync" class which inherited from "BaseEntity" class. Following is my class structure.

 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public class ApplicationSetting : BaseSyncEntity 
 {
    [DataMember]
    public string SettingKey { get; set; }
 }



 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public abstract class BaseSyncEntity : BaseEntity
 {

 }
 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public abstract class BaseEntity : ICloneable, IComparable, IEntityState,        INotifyPropertyChanged, IDataErrorInfo
 {
    [DataMember(EmitDefaultValue = false)] 
    public Nullable<int> Id
    {
        get
        {
            return _id;
        }
        set
        {
            SetProperty(ref _id, value);  
        }
    }
    [DataMember(EmitDefaultValue = false)]
    public EntityState EntityState
    {
        get { return _entityState; }
        set { _entityState = value; }
    }

 }

Now problem is that whenever i check my class json object in browser. I don't get my Enum property which is EntitySate? Any reason why? I am getting all other property like SettingKey and Id but i'm not getting my Enum at all.

My enum class structure is mentioned below

public enum EntityState
    {
        /// <summary>
        /// The Business Entity is unchanged and no action needs to be taken on it.
        /// </summary>
        Unchanged,

        /// <summary>
        /// The Business Entity has been created and has not been inserted into the database.
        /// </summary>
        Added,

        /// <summary>
        /// The Business Entity has been changed and requires updating in the database.
        /// </summary>
        Changed,

        /// <summary>
        /// The Business Entity has been marked for removal from the database.
        /// </summary>
        Removed
    }

You need to define your ENUM as data contract.

Some thing like following.

[DataContract(Name = "CarCondition")]
public enum CarConditionWithNumbers
{
    [EnumMember]
    New = 10,

    [EnumMember]
    Used = 20,

    [EnumMember]
    Rental = 30,
}

see following link.

http://msdn.microsoft.com/en-us/library/aa347875.aspx

Also there few other examples also.

http://beyondrelational.com/modules/2/blogs/48/posts/10065/enumeration-in-datacontract-of-wcf.aspx

http://dotnetmentors.com/wcf-datacontract-with-enum-example.aspx

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