How to pass object to javascript function in onclick event?

0

from a list of objects, go through each, and for each row, pass the complete object to a javascript function, where you can access the data and eventually convert that object to json. In my attempts I only get [object].

The idea is to pass the whole object in a javascript function

$.ajax({
    url: "@Url.Action("ListOfCars")",
    type: "POST",
    data: JSON.stringify(parameters),
    dataType: "JSON",
    contentType: "application/json",
    success: function (data) {
        if ($.trim(data)) {
            $(data).each(function () {
                $("#car_rows").append("<tr>" + "<td>" + this.ID + "</td>" +
                "<td><a href="#" onclick="functionTest(\'' + data + '\');">Ver Detalle</a>' +</td>" +
                '</td>'+ "</tr>"
            }
        }
    }
})
functionTest(obj) {
    var test = JSON.stringify(obj);
}

In the test variable I can not access any data. Any solution?

Example Json:

[
  {
    "ID": "777846730",
    "OrderDate": "2018-08-27T00:00:00",
    "AccountClient": "55474",
    "DaysToClaim": 1,
    "Courier": {
      "ID": "BEX",
      "Name": "Express",
      "Active": false
    },
    "CustomerOrder": {
      "OrderNbr": "78577",
      "DocumentNbr": "1117",
      "Date": "2018-08-26T00:00:00",
      "PaymentType": "MC",
      "TotalAmount": "14444"
    },
    "DeliveryAddress": {
      "Address": "CAMINO A BUENA PAZ~6",
      "Number": null,
      "References": ""
    },
    "Customer": {
      "ID": "524",
      "DocumentNbr": "",
      "Name": "JOSE AGUSTIN PARDO ORELLANA",
      "Phone": "979797"
    },
    "ClaimDetail": {
      "DateClaim": "2018-09-06T15:28:07",
      "Observation": "test 1",
      "User": "user1"
    },
    "LastDeliveryStatus": {
      "ID": "OK",
      "Name": "OK",
      "Date": "2018-09-13T13:21:37",
      "Description": "test 1",
      "GlobalStatus": {
        "ID": null,
        "Name": null,
        "IsActive": false,
        "TotalOrder": 0,
        "Icon": null,
        "Color": null,
        "Orders": []
      },
      "Detail": {
        "PostalNbr": null,
        "PostalName": "Matriz"
      }
    }
  }
]

Which is being returned with the following line:

return Json(list, JsonRequestBehavior.AllowGet);
    
asked by Dmz 12.09.2018 в 23:57
source

1 answer

0

I think you have a syntax error in the concatenation of your data variable.

"<td><a href='#' onclick='functionTest('" + data + "');'>Ver Detalle</a></td>"

Try something like this, and tell me how it goes.

    
answered by 13.09.2018 в 00:11