您从控制器操作(使用)返回的JsonResult操作在
returnJson(...)内部依赖于JavascriptSerializer类。此类不考虑
DataMember模型上的任何属性。
您可以编写一个自定义ActionResult,该
System.Runtime.Serialization.Json名称在名称空间中使用序列化程序。
例如:
public class MyJsonResult : JsonResult{ public override void ExecuteResult(ControllerContext context) { var response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } if (Data != null) { var serializer = new DataContractJsonSerializer(Data.GetType()); serializer.WriteObject(response.OutputStream, Data); } }}然后在您的控制器操作中:
public ActionResult Foo(){ var model = ... return new MyJsonResult { Data = model };}


