I have an ASP.NET MVC 5.2.7 project (not Core)
public ActionResult Magic()
{
MyType model = BuildModel();
return View("Index", model);
}
[HttpPost]
public ActionResult Magic(MyType model)
{
}
public class MyType
{
[JsonConverter(typeof(MyConverter<MySubType>))]
public TySubType Value { get; set; }
}
[DataContract]
[JsonConverter(typeof(MyConverter<MySubType>))]
public enum MySubType
{
[EnumMember("v1")]
MyV1
}
public class MyConverter<T> : JsonConverter
where T: Enum
{
public override bool CanConvert(Type objectType) => objectType is T;
public override bool CanRead => true;
public override bool CanWrite => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {}
}
With this configuration, my WriteJson
is called on GET, but not used on POST to deserialize the body. What should be changed here?
In the end, I'm trying to get it using the EnumMember
value to serialize/deserialize enums