How to tell JSON.NET StringEnumConverter to take DisplayName?

前端 未结 3 1936
死守一世寂寞
死守一世寂寞 2021-02-02 12:31

I\'ve got the following model:

public enum Status
{
    [Display(Name = \"Awaiting Approval\")]
    AwaitingApproval,
    Rejected,
    Accepted,
}
3条回答
  •  迷失自我
    2021-02-02 13:04

    I tried this and got the error type or namespace enum member could not be found... So you guys might get this error too so you need to use

    using System.Runtime.Serialization;
    

    and still you get this error then add a reference like below:

    Right click on your project -> Add -> Reference.. -> Assemblies -> Mark System.Runtime.Serialization (i have 4.0.0.0 version ) -> Ok
    

    Now you can proceed like this:

    [JsonConverter(typeof(StringEnumConverter))]
    public enum Status
    {
        [EnumMember(Value = "Awaiting Approval")]
        AwaitingApproval,
        Rejected,
        Accepted,
    }
    

提交回复
热议问题