Steven Frank
Steven Frank

Reputation: 621

Reading responseJson from MVC Controller

I have an MVC controller that returns Json results:

return Json(elements);

elements is a List

This is returned from an ajax call and I am processing the results in the OnComplete call.

In that call, I have everything I need, I can looks at the results and see the array of strings in responseJson but I can't figure out the syntax to get at the array.

function OverviewElementRemove_Complete(result) {
    $.each(result.responseJson, function (i, e) {
        //do some stuff?
    });
}

Again, to be clear, I can see the results I want in the debugger, I just don't know the syntax to loop through responseJson.

I know there are many questions similar to this but none cover this scenario.

Upvotes: 0

Views: 96

Answers (2)

Steven Frank
Steven Frank

Reputation: 621

Fort those who didn't or couldn't read my original answer, here it is again.

Referring to "responseJson" (note the case) instead of "responseJSON" (again, note the case) is the problem.

Once I changed the reference to "responseJSON" (again, again, note the case) it worked.

Upvotes: 0

dionm
dionm

Reputation: 43

I might be missing something, but wouldn't result in your JS function be the list of strings? i.e.

result === ['string1', 'string2', 'etc.']

That's what i seem to get when i ran a basic test returning this:

return Json(new List<string>{"string1", "string2"});

So to loop through each item your code should just be:

function OverviewElementRemove_Complete(result) {
    $.each(result, function (i, e) {
        //do some stuff?
    });
}

Upvotes: 1

Related Questions