I have a problem with ASP.Net MVC3 (RC2). I found that the new JSON model binding function, which is implicit in MVC3, does not want to deserialize an enumeration type property.
Here is an example of a class and enumeration type:
public enum MyEnum { Nothing = 0, SomeValue = 5 }
public class MyClass
{
public MyEnum Value { get; set; }
public string OtherValue { get; set; }
}
Consider the following code that successfully passes the unit test:
[TestMethod]
public void Test()
{
var jss = new JavaScriptSerializer();
var obj1 = new MyClass { Value = MyEnum.SomeValue };
var json = jss.Serialize(obj1);
var obj2 = jss.Deserialize<MyClass>(json);
Assert.AreEqual(obj1.Value, obj2.Value);
}
If I serialize obj1above, but then send this data to the MVC3 controller (example below) with a single parameter of type MyClass, any other properties of object deserialization properly, but any property that is an enumeration type, will deserialize to the default value (zero).
[HttpPost]
public ActionResult TestAction(MyClass data)
{
return Content(data.Value.ToString());
}
MVC codeplex, , , , , , Microsoft , , , - .
.