asp.net-mvc – MVC4 Action返回没有null的JsonResult
发布时间:2020-12-16 04:25:42 所属栏目:asp.Net 来源:网络整理
导读:我有一个动作,它返回一个特定类的对象的JsonResult.我已经使用一些attrib来装饰这个类的属性以避免使用null字段.类定义是: private class GanttEvent { public String name { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] pu
我有一个动作,它返回一个特定类的对象的JsonResult.我已经使用一些attrib来装饰这个类的属性以避免使用null字段.类定义是:
private class GanttEvent { public String name { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public String desc { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public List<GanttValue> values { get; set; } } 在我的动作中我使用了一个对象 var res = new List<GanttEvent>(); 我回来使用: return Json(res,JsonRequestBehavior.AllowGet); 不幸的是,我仍然在输出时收到空值: [{"name":"1.1 PREVIOS AL INICIO ","desc":null,"values":null},{"name":"F04-PGA-S10","desc":"Acta preconstrucción",{"name":"F37-PGA-S10","desc":"Plan de inversión del anticipo",{"name":"F09-PGA-S10","desc":"Acta de vecindad",{"name":"F05-PGA-S10","desc":"Acta de inicio",{"name":"F01-PGA-S10","desc":"Desembolso de anticipo","values":null}] 我错过了什么或做错了什么? 解决方法
正如Brad Christie所说,MVC4仍然使用JavaScriptSerializer,因此为了让你的对象被Json.Net序列化,你将不得不执行几个步骤.
首先,从JsonResult继承一个新类JsonNetResult,如下所示(基于this solution): public class JsonNetResult : JsonResult { public JsonNetResult() { this.ContentType = "application/json"; } public JsonNetResult(object data,string contentType,Encoding contentEncoding,JsonRequestBehavior jsonRequestBehavior) { this.ContentEncoding = contentEncoding; this.ContentType = !string.IsNullOrWhiteSpace(contentType) ? contentType : "application/json"; this.Data = data; this.JsonRequestBehavior = jsonRequestBehavior; } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); var response = context.HttpContext.Response; response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data == null) return; // If you need special handling,you can call another form of SerializeObject below var serializedObject = JsonConvert.SerializeObject(Data,Formatting.None); response.Write(serializedObject); } } 然后,在您的控制器中,重写Json方法以使用新类: protected override JsonResult Json(object data,JsonRequestBehavior behavior) { return new JsonNetResult(data,contentType,contentEncoding,behavior); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 逗号之后拆分字符串直到字符串结束 – asp.net c#
- 对于ASP.Net中的IIS管理哪个更好:WMI或ADSI或托管API?有什
- asp.net-mvc-3 – ViewBag在Extension Class中返回null
- .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓
- 在ASP.NET 2.0中的特定时间清除缓存
- asp.net-mvc-3 – DotNetOpenAuth.Asp在MVC4应用程序单元测
- asp.net-mvc – ASP.NET Web API – Windows窗体应用程序中
- asp.net-mvc – 具有多个路由值的Url.Action帮助器的问题
- asp.net – 如何为ValidateAntiForgeryToken选择一个盐值
- asp.net-mvc – ASP.NET MVC和IIS 5
推荐文章
站长推荐
- [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的
- asp.net – AppHarbor:不允许使用目录’/ App_G
- asp.net-mvc – MvcContrib.MVC3-ci和Mvc.Contri
- 【开源小软件 】Bing每日壁纸 让桌面壁纸保持更新
- ASP.NET MVC – 操纵HTTP Post
- 部署 – Crystal报表文件未部署到ASP.NET MVC中的
- asp.net-mvc – 在visual studio中快速更改视图和
- 使用Openiddict的ASP.NET Core 1.0 OAuth服务器
- asp.net – 如何使用JWT对角度SPA的Web Api 2进行
- asp.net-mvc-2 – 关于nginx / mono 2.8的ASP.Ne
热点阅读