Just in case. Screen charts cannot display a chart if the value is set to zero. And the Json method of asp.net mvc mpc class cannot filter null value.
To do this, you can use the json.net library and create, for example, JsonNetResult (inherit from ActionResult):
public class JsonNetResult : ActionResult { public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } public JsonSerializerSettings SerializerSettings { get; set; } public Formatting Formatting { get; set; } public JsonNetResult() { SerializerSettings = new JsonSerializerSettings(); } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); HttpResponseBase response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data != null) { JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } }
and then add this method to your controller to replace the asp.net mvc Json method:
protected JsonNetResult JsonNet(object data, bool needDefaultSettings) { var result = new JsonNetResult(); result.Data = data; if (needDefaultSettings) { var defaultSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }; result.SerializerSettings = defaultSettings; } return result; }
So now you can use it in your action to control as follows:
public JsonNetResult MyAction() { MyClass myObject = new MyClass(); return JsonNet(myObject); }
And also, feel free to use the Json.Net DefaultValue attribute for MyClass properties:
[DefaultValue(null)]
source share