【问题标题】:String property not deserialized字符串属性未反序列化
【发布时间】:2015-10-23 04:52:29
【问题描述】:

我不明白为什么我的 ASP.NET MVC5 控制器中只有字符串属性 Search.Value 没有被反序列化。请看这个:

客户端发送的Json结构:

{
   "draw":1,
   // ...
   "start":0,
   "length":50,
   "search":{
      "value":"This is always null in my controller",
      "regex":false
   }
}

我有服务器端的模型:

public class AsyncDataTableRequest
{
    public int Draw { get; set; }
    public int Start { get; set; }
    public int Length { get; set; }
    public Search Search { get; set; }
    public string Value { get; set; }
}

public class Search
{
    public string Value { get; set; }
    public bool Regex { get; set; }
}

我想用 Search.Value 做某事的控制器:

public JToken AsyncLogFetching(AsyncDataTableRequest req)
{
    // req.Search.Value is null here, all other properties seem correct
    ...
}

感谢您的帮助!

编辑: 对于使用“NewYork”的示例搜索,这是来自 IE 开发者工具中“请求标头”选项卡的请求:

GET /Log/AsyncLogFetching?draw=3&start=0&length=50&search%5Bvalue%5D=NewYork&search%5Bregex%5D=false&_=1438350434912 HTTP/1.1

IE 开发工具中的“请求文本”选项卡显示“没有数据可显示”。

这是执行 GET 请求的 sn-p,它是从 jQuery DataTables Pipelining example 复制和粘贴的:

settings.jqXHR = $.ajax({
    "type":     conf.method, // GET
    "url":      conf.url,
    "data":     request,
    "dataType": "json",
    "contentType": "application/json",
    "cache":    false,
    "success": function (json) {
        // ...
    }
});

【问题讨论】:

  • 您能看到正在发布的搜索内容吗(例如在 Chrome devtools 网络选项卡中)?要求此检查问题是否出在接收部分的发送中。您说“客户端发送的 Json 结构......”您真的在控制器的请求中看到了吗?
  • 嗨,米歇尔,请看我的编辑 - 搜索肯定有内容。
  • 您是在进行 GET 还是 POST?我不确定 GET 请求是否会进行相同的反序列化
  • 大多数示例使用 POST,类似这样:` $.ajax({ url: url, type: 'POST', dataType: 'json', data: JSON.stringify(data), contentType: '应用程序/json; charset=utf-8' }); `
  • 不错。很高兴能提供帮助...

标签: javascript c# asp.net json


【解决方案1】:

尝试使用 JSON.stringify(yourObject) 将数据从客户端发送到控制器

请参考以下 How to send nested json object to mvc controller using ajax

【讨论】:

  • 不幸的是,这没有效果。它仍然是空的,但感谢您的回复
  • 阿米特,如果我添加 JSON.stringifry(req) 然后我在控制器中收到一个空模型(int-properties 为 0,字符串为空)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多