0

How to get value of Object from List in jQuery? Ath the moment I'm getting List of ActionResults from controller and I want to get value of each action (html code).

There is't a problem when I try to send single ActionResult - without list.

When I try to send List<ActionResult> I get object Object every time.

<script type="text/javascript">
    $(document).on('click', 'button', function () {
        $cartAction = $("#" + this.id);
        var url = $cartAction.data("url");
        $.get(url, function (actions) {
           console.log(actions); <-- return Objects Array
           console.log(???); <-- how to get value of each actions
           console.log(actions[1]); <-- return a single object
           console.log(actions[1].document) <-- value undefined
        });
    });
</script>
public ActionResult Index()
{
    var actionList = new List<ActionResult>()
    {
        // ViewComponents, PartialViews
    };
    return Json(actionList);
}

I expect return html code for each ActionResult, but I get

[object Object],[object Object]

for actions list.

Edit: Answer on my question is here stackoverflow.com/a/53639387/10338470

Lukson
  • 13
  • 5
  • 1
    Loop over the `actions` array. Don't use `alert` for debugging. Log to console and you will see exactly what you are working with – charlietfl Mar 24 '19 at 11:09
  • Do not return `List` from action. Return `ActionResult` and at the end of method call `return Json(collection)`. In js browse object properties – Alexander Mar 24 '19 at 11:10
  • After change on Json(collection) result is the same - undefined value – Lukson Mar 24 '19 at 11:50
  • Maybe there's no document key in index 1? Does `actions.forEach(action => {console.dir(action);})` gives you any results? – eyecatchUp Mar 24 '19 at 12:25
  • I tried foreach loop and for every elements of list I get `Object` in console, but I want to get value this `Object` (html code) – Lukson Mar 24 '19 at 12:30
  • In this case, `actions` is not a valid array of objects. Before the return statement add `string json = JsonConvert.SerializeObject(actionList);`, then log it `Console.WriteLine(json);` and check if it is valid. – eyecatchUp Mar 24 '19 at 12:49
  • I checked it and `json` was a string with `Object` elements like Arguments, Model etc. – Lukson Mar 24 '19 at 13:08
  • So, what If you return the `json` string instead of `Json(actionList)` and use `JSON.parse(actions)` in JS? Would that work? – eyecatchUp Mar 24 '19 at 13:20
  • No, I want to get html code of ViewComponent, PartialView etc., no html code of object arguments. – Lukson Mar 24 '19 at 14:35
  • Answer on my question is here https://stackoverflow.com/a/53639387/10338470 – Lukson Mar 24 '19 at 18:01

1 Answers1

0

In jQuery you can iterate through the Values like this

<script type="text/javascript">
    $(document).on('click', 'button', function () {
        $cartAction = $("#" + this.id);
        var url = $cartAction.data("url");
        $.get(url, function (actions) {
           console.log(actions); // <-- return Objects Array
           $.each(function() { // <-- iterates through each object in actions array
               console.log($(this)); // <-- logs the object to the console
           });
           console.log(actions[1]); // <-- return a single object
           console.log(actions[1].document) // <-- value undefined
        });
    });
</script>

In most browsers even console.log(actions); would work. But you can always JSON.stringify(actions);

Marius Steinbach
  • 548
  • 7
  • 19