System.Web.Mvc.JsonResult use the old JavaScriptSerializer class that knows nothing about the DataAnnotiations assembly. You need to use DataContractJsonSerializer .
You can use this instead of JsonResult if you want:
public class DataContractJsonResult : JsonResult { public DataContractJsonResult(object data) { Data = data; } public DataContractJsonResult() { } public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); } HttpResponseBase response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { var serializer = new DataContractJsonSerializer(Data.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, Data); string json = Encoding.UTF8.GetString(ms.ToArray()); response.Write(json); } } }
(I refer to the source code of ASP.NET MVC to create this. Not sure if I need to thank him in any way. Well, more than that already exists, that is. :))
You can also add this to the base class from which your controllers inherit:
protected JsonResult DataContractJson(object data) { return new DataContractJsonResult(data); }
source share