0

I have a table where I am attempting to get values from a clicked row onto a modal box. My data is being returned to the view, but I have issues accessing the property values of my object.

This is my currency code
Script

$('.item-list tr td').click(function () {
    var clickedRow = $(this).closest("tr").find('td:eq(5)').text();

    $.ajax({
        "url": "/ActivityType/Details",
        "type": "POST",
        "dataType": "JSON",
        "data": { aActivityID: clickedRow },
        "success": function (data) {
            //var thisData = JSON.parse(data);
            console.log(data);

            console.log(data.activityName);                
            console.log(data["activityName"]);
            console.log(data['activityName']);

            document.getElementById("ActivityName").value = data[0].activityName;
            showAjaxModal();
        }
    });
});

Controller

public IActionResult Details(System.Guid aActivityID)
    {
        if (aActivityID != System.Guid.Empty)
        {
            t_activitytype aActivity = new t_activitytype();
            aActivity = db.t_activitytype.Where(a => a.Guid == aActivityID).First();

            return Json(new { data = aActivity });
        }
        return View();
    }

console.log(data); shows my object with its property values enter image description here

trying to access the properties at this point returns 'undefined' for the following lines

console.log(data.activityName); console.log(data["activityName"]); console.log(data['activityName']);

enter image description here

3
  • 7
    It looks like the structure might be data.data.activityName. Commented Jul 10, 2019 at 20:35
  • 1
    Notice in your console log, the first (and only) property off of the element you logged is data Commented Jul 10, 2019 at 20:35
  • 2
    So instead of return Json(new { data = aActivity }); just do return Json(aActivity) Commented Jul 10, 2019 at 20:37

1 Answer 1

1

The controller includes a data property in the data itself. You can actually see that addition data in the console. So probably you would better remove that extra layer at the server side.

Instead of

return Json(new { data = aActivity }); 

just do

return Json(aActivity)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks trincot, that solved it. I will keep that in mind, that the controller includes a data property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.