asp.net mvc3 return multiple json list

ajax, asp.net-mvc-3, c#, jquery

Solution

This alerts the value of `Name` from each record of each list.

$.each(result, function (i, item) {
    for (var x = 0; x < result.FirstList.length; x++) {
        alert(result.FirstList[x].Name);
        alert(result.SecondList[x].Name);
    }
});

This assumes your Json response if formed correctly. Like this:

return Json(new { FirstList = results, SecondList = otherResults }, JsonRequestBehavior.AllowGet);

But as a side note, I see other problems with your code that you need to address

- You're actually not performing a `POST`, you're searching based on input. Change `POST` to `GET` in your Ajax call

- Change your action return line to allow for the get and make sure your are returning a `JsonResult`.

Naming conventions for C# method parameters call for Pascal-casing. Use a lowercase letter for first character

public JsonResult Search(string searchText) {
    ....
    return Json(new { name = UserNames, imageUrl = ImageUrls }, JsonRequestBehavior.AllowGet);
}

Problem

Im asp.net mvc3 c# code returns json list like this: ``` return Json(new { name = UserNames, imageUrl = ImageUrls }); ``` `UserNames` and `ImageUrls` are both `List<string>` types And this is my javascript ``` function StartSearch(text) { $.ajax({ url: '/Shared/Search', type: 'POST', data: { SearchText: text }, dataType: 'json', success: function (result) { $.each(result, function (i, item) { alert(result[i].name); }); } }); } ``` How I can get names and `ImageUrls`? Thanks

Original source