Question

I am not sure why my enum of the type ScheduleType doesnt bind to the property Type in the json below. The other properties bind find, and I havent had an issue with an enum binding in other places, thoughts? It always defaults the the first one.

JSON

{"Id":0,"BulkInsertId":null,"DivisionId":10406,"DivisionName":"17","DivisionOrder":1,"Type":1,"Name":"A1 vs. A2" }

Class

public class ScheduleMatchupModel : IScheduleMatchupModel
    {
        public ScheduleType Type { get; set; }

        public int Id { get; set; }
        public int DivisionId { get; set; }
        public int? DivisionOrder { get; set; }
        public string DivisionName { get; set; }

IScheduleMatchupModel.cs

public interface IScheduleMatchupModel
{
    int Id { get; set; }
    ScheduleType Type { get; set; }

ScheduleType Enum

[DataContract(Namespace = "")]
public enum ScheduleType
{
    [EnumMember(Value = "0")]
    All = 0,
    [EnumMember(Value = "1"), Display(Name = "Pool Play")]
    Pool,
    [EnumMember(Value = "2"), Display(Name = "Bracket Play")]
    Bracket
}
Was it helpful?

Solution

By means of the EnumMember attribute you set certain values to your enums:

[EnumMember(Value = "0")]
All = 0,
[EnumMember(Value = "1"), Display(Name = "Pool Play")]
Pool,
[EnumMember(Value = "2"), Display(Name = "Bracket Play")]
Bracket

The only problem is that the Value property of EnumMember attribute is a string:

public string Value { get; set; }

And you assign integers to your enums in your JSON:

{...,"Type":1,... }

You should therefore try to change it to string in your JSON:

{..."Type":"1",... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top