0

I have this property ScheduleDatePeriod as a enum on this EditEventScheduleSettingsModel class. I serialize as JSON to the page within javascript which makes the property Period: 1. When I submit this to the server as Period: 1, the enum is always null. Why is this?

Property

public ScheduleDatePeriod? Period { get; set; }

Action

 [HttpPost]
        public virtual ActionResult Grid(int eventId, EditEventScheduleSettingsModel settings)
        {

JSON

{
   Period: 1
}

Enum

[DataContract(Namespace = "")]
public enum ScheduleDatePeriod
{
    [EnumMember(Value = "0"), Display(Name = "None")]
    None = 0,
    [EnumMember(Value = "1"), Display(Name = "Day")]
    Day = 1,
    [EnumMember(Value = "2"), Display(Name = "Week")]
    Week = 2
}

ValueProviderFactory

    public class JsonNetValueProviderFactory : ValueProviderFactory
    {
        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
        {
            // first make sure we have a valid context
            if (controllerContext == null)
                throw new ArgumentNullException("controllerContext");

            // now make sure we are dealing with a json request
            if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
                return null;

            // get a generic stream reader (get reader for the http stream)
            var streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
            // convert stream reader to a JSON Text Reader
            var jsonReader = new JsonTextReader(streamReader);
            // tell JSON to read
            if (!jsonReader.Read())
                return null;

            // make a new Json serializer
            var jsonSerializer = new JsonSerializer();
            jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            // add the dyamic object converter to our serializer
            jsonSerializer.Converters.Add(new ExpandoObjectConverter());

            // use JSON.NET to deserialize object to a dynamic (expando) object
            Object jsonObject;
            // if we start with a "[", treat this as an array
            if (jsonReader.TokenType == JsonToken.StartArray)
                jsonObject = jsonSerializer.Deserialize<List<ExpandoObject>>(jsonReader);
            else
                jsonObject = jsonSerializer.Deserialize<ExpandoObject>(jsonReader);

            // create a backing store to hold all properties for this deserialization
            var backingStore = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            // add all properties to this backing store
            AddToBackingStore(backingStore, String.Empty, jsonObject);
            // return the object in a dictionary value provider so the MVC understands it
            return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture);
        }

        private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value)
        {
            var d = value as IDictionary<string, object>;
            if (d != null)
            {
                foreach (KeyValuePair<string, object> entry in d)
                {
                    AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);
                }
                return;
            }

            var l = value as IList;
            if (l != null)
            {
                for (int i = 0; i < l.Count; i++)
                {
                    AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);
                }
                return;
            }

            // primitive
            backingStore[prefix] = value;
        }

        private static string MakeArrayKey(string prefix, int index)
        {
            return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";
        }

        private static string MakePropertyKey(string prefix, string propertyName)
        {
            return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName;
        }
    }

Global.asax

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • possible duplicate of [How to get the Display Name Attribute of an Enum member via MVC razor code?](http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) – MethodMan Aug 15 '14 at 18:47
  • I don't want name value, and has nothing to do with name value – Mike Flynn Aug 15 '14 at 18:53

2 Answers2

0

I did some testing of the JsonNetValueProviderFactory and I think I have found the cause of the problem.

The following line will parse the "Period" value as a long and not an int. The value of the enum is an int, causing the conversion to the enum to fail.

jsonObject = jsonSerializer.Deserialize<ExpandoObject>(jsonReader);

I can't really find a good solution for this, but these are workarounds

  • Pass in period as a string value instead
  • Use the default JsonValueProviderFactory
andreas
  • 505
  • 1
  • 7
  • 16
  • Which Json library did you use? OP's problem seems in deserialzation, where is that part in your answer? – L.B Aug 15 '14 at 19:29
  • Absolutely right. I am using JSON.NET Provider Factory. I wonder why it doesnt work the same as the .NET version...I posted the code in case you wanted to look at it – Mike Flynn Aug 15 '14 at 19:29
  • For some reason it just wont serialize the value 1 to the enum value. The expando object and dictionary have the correct value. – Mike Flynn Aug 15 '14 at 20:08
  • 1
    Long story short, I added `[JsonConverter(typeof(StringEnumConverter))]` and it worked. Wish I could just use the value but this will work for now. – Mike Flynn Aug 15 '14 at 20:46
0

I am not sure if you provided full JSON that you used, but I tested your example with next JSON and enum was successfully deserialized (tested in fiddler).

{
    "eventId": 10,
    "settings": 
       {
        "Period" : 2
       }
}

Please note that you should use "settings" in JSON to match name of parameter in your method.

ntl
  • 1,289
  • 1
  • 10
  • 16
  • `I am not sure if you provided full JSON` It is a valid json. No need for extra *settings* property. – L.B Aug 15 '14 at 19:26
  • You are right, I've already checked with JSON from question description and it works fine. – ntl Aug 15 '14 at 19:33