MVC4行动返回JsonResult没有空 [英] MVC4 Action returning JsonResult without null

查看:111
本文介绍了MVC4行动返回JsonResult没有空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有返回JsonResult特定类的对象的动作。我装饰了这个类的属性与一些ATTRIB,以避免空字段。类定义是:

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>();

我将带回使用:

    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?

推荐答案

如前所述布拉德·克里斯蒂,MVC4剧照使用的JavaScriptSerializer,所以为了让你的对象由Json.Net序列化,你将必须执行几个步骤。

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.

首先,继承JsonResult一个新的类JsonNetResult如下(根据<一个href=\"http://stackoverflow.com/questions/6883204/change-default-json-serializer-used-in-asp-mvc3\">this解决方案):

First, inherit a new class JsonNetResult from JsonResult as follows (based on 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的方法来使用新的类:

Then, in your controller, override the Json method to use the new class:

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonNetResult(data, contentType, contentEncoding, behavior);
}

这篇关于MVC4行动返回JsonResult没有空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆