简体   繁体   中英

JSON object passed to Controller is NULL

I understand this question has been posted a thousand times, but I have tried every solution to no avail.

I have this JSON object:

{ 
  "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"
}

I pass this JSON object to the controller which is just

Function submitMonitor(monitor As Monitor)
  Return False
End Function

The ajax call to pass to the controller is as follows:

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

I have tried passing:

data:data

I have tried without the ajaxSettings.traditional line. I have tried with the commented blocks on or off, but still no luck.

Here is the class that it should be passing to.

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

Here is the class that Monitor inherits:

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

Whenever the controller gets called though, looking at the monitor variable in debugger, I see that all the values are set to Nothing.

Any idea what could be causing this?

Based upon the data you are showing in JSON. You need to set monitor

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM