【问题标题】:JSON object passed to Controller is NULL传递给 Controller 的 JSON 对象为 NULL
【发布时间】:2015-08-26 04:00:44
【问题描述】:

我知道这个问题已经发布了一千次了,但是我尝试了所有的解决方案都无济于事。

我有这个 JSON 对象:

{ 
  "manufacture":"HP",
  "model":"testModel",
  "serialNumber":"testSerial",
  "description":"Test Description",
  "checkIn":true,
  "stand":false,
  "tilt":true,
  "pivot":false,
  "heightAdjust":true,
  "size":27,
  "portTypes":"2 VGA",
  "resolution":"1080P"
}

我将这个 JSON 对象传递给控制器​​,它只是

Function submitMonitor(monitor As Monitor)
  Return False
End Function

传递给控制器​​的ajax调用如下:

jQuery.ajaxSettings.traditional = true;
$.ajax({
    url: '@Url.Action("submitMonitor", "Home")',
    type: 'GET',
    contentType: 'application/json',
    //contentType: "application/json; charset=utf-8",
    //dataType: 'json',
    //processData: true,
    data: JSON.stringify(data)
});

我已经尝试过:

data:data

我尝试过不使用 ajaxSettings.traditional 行。我已经尝试打开或关闭注释块,但仍然没有运气。

这是它应该传递给的类。

Public Class Monitor : Inherits HardwareData
   Public stand As Boolean
   Public tilt As Boolean
   Public pivot As Boolean
   Public heightAdjust As Boolean
   Public size As Integer
   Public portTypes As String
   Public resolution As String
End Class

这是 Monitor 继承的类:

Public Class HardwareData
   Public key As Integer
   Public manufacture As String
   Public model As String
   Public serialNumber As String
   Public createdBy As String
   Public createdTimeStamp As Date
   Public description As String
   Public checkIn As Boolean
End Class

不过,每当调用控制器时,查看调试器中的监控变量,我发现所有值都设置为 Nothing。

知道是什么原因造成的吗?

【问题讨论】:

  • 你试过设置 contentType: 'application/json', dataType: 'json',?
  • 是的,那也没用。仍然将所有内容作为 Nothing 传递给控制器​​,但查看 Chrome 的检查器网络选项卡中的调用,在 get 调用中所有内容都是有效的 JSON。

标签: javascript asp.net ajax vb.net asp.net-mvc-3


【解决方案1】:

基于您在 JSON 中显示的数据。你需要设置monitor

$.ajax({
    url: '@Url.Action("submitMonitor", "Home")',
    type: 'GET',
    contentType: 'application/json',
    data: {monitor:data}
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-28
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    相关资源
    最近更新 更多