Rubens Mussi Cury Rubens Mussi Cury - 4 months ago 28
C# Question

Using JsonConverter for enum element value

After reading this post JSON serialization of enum as string I'm still searching for a quick way to use into Razor javascript this type of Enum:

public enum StatusReplacement
ApprovalPending = 1,
Canceled = 2,
Approved = 3,
AwaitingDelivery = 4,
Delivered = 5,
Completed = 6

By using JsonConverter I just can take the element enums but not their values. I also tried unsuccessfully to set over each element [EnumMember(Value = "")].

Expected result

var elementValue = @StatusReplacement.ApprovalPending;
alert(elementValue) //Expecting to return 1 instead of ApprovalPending *undefined.

I'm not sure yet if I really have use any king of html helper for this purpose.
I suspicious there is an easier way to achieve it today working with MVC 4+.



The JSON formatter takes preference on the output of the data and provides a string output of the enum value itself:

var data = "@StatusReplacement.ApprovalPending"; // = ApprovalPending

You should use

var data = @((int)StatusReplacement.ApprovalPending); // = 1

to explicitly use the int value.