I have a action which returns an JsonResult for an object of a particular class. I have decorated the properties of this class with some attrib to avoid null fields. Class definition is:
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; }
}
And in my Action i use an object
var res = new List<GanttEvent>();
which I return using:
return Json(res, JsonRequestBehavior.AllowGet);
Unfortunatelly, I’m still receiving null values at output:
[{"name":"1.1 PREVIOS AL INICIO ","desc":null,"values":null},{"name":"F04-PGA-S10","desc":"Acta preconstrucción","values":null},{"name":"F37-PGA-S10","desc":"Plan de inversión del anticipo","values":null},{"name":"F09-PGA-S10","desc":"Acta de vecindad","values":null},{"name":"F05-PGA-S10","desc":"Acta de inicio","values":null},{"name":"F01-PGA-S10","desc":"Desembolso de anticipo","values":null}]
Am I missing something or doing something wrong?
As stated by Brad Christie, MVC4 stills uses JavaScriptSerializer, so in order to get your object serialized by Json.Net, you will have to perform several steps.
First, inherit a new class JsonNetResult from JsonResult as follows (based on this solution):
Then, in your controller, override the Json method to use the new class: