4

我 asp.net mvc3 c# 代码返回 json 列表,如下所示:

return Json(new { name = UserNames, imageUrl = ImageUrls });

UserNames并且ImageUrls都是List<string>类型

这是我的 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);
            });
        }
    });
}

我怎样才能得到名字和ImageUrls

谢谢

4

2 回答 2

5

Access name as a property of result like this result.name[i]

Essentially, result will contain name and imageUrl which are both arrays, just like you have defined in your anonymous type, so your code should be modified like this to display an alert for each name in the name array

function StartSearch(text) {
    $.ajax({
        url: '/Shared/Search',
        type: 'POST',
        data: { SearchText: text },
        dataType: 'json',
        success: function (result) {
            $.each(result.name, function (i, item) {
                alert(item);
           });
        }
    });
}

as $each iterates over the items in the name array, it will pass the item to the second parameter of your call back, i.e. item.

so

$.each(result.name, function (i, item) {
    alert(item);
});

will popup each name.

Notes:

You may want to change the properties on your anonymous type to reflect that they are a collection:

return Json(new { UserNames = UserNames, ImageUrls = ImageUrls });

this way it will make more sense when you iterate over them in your success function.

As AlfalfaStrange pointed out, I didn't demonstrate how you might access both arrays. Which made me think, what is the relationship between user names and image urls?

Is this a list of images for a user? Maybe what you should consider is creating a specific model for this. For instance, UserDisplayModel:

public class UserDisplayModel
{
    public string UserName {get;set;}
    public string ImageUrl {get;set;}
}

In your controller return a list of UserDisplayModels. If this is the case, you'll have to rethink why they are two separate lists in the first place. Maybe ImageUrl should be a field on the User table.

So now, when you're returning a single list, e.g.

List<UserDisplayModel> users = //get users from db
return Json(new { Users = Users});

you can iterate them in one go in js code:

       $.each(result.Users, function (i, item) {
            alert(item.Name);
            alert(item.ImageUrl);
        });
于 2012-06-03T10:37:28.527 回答
4

这会提醒Name来自每个列表的每个记录的值。

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

如果格式正确,这假设您的 Json 响应。像这样:

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

但作为旁注,我发现您的代码存在其他需要解决的问题

  1. 您实际上并没有执行 a POST,而是根据输入进行搜索。在您的 Ajax 调用中更改POSTGET
  2. 更改您的操作返回行以允许获取并确保您返回的是JsonResult.
  3. C# 方法参数的命名约定要求使用 Pascal 大小写。第一个字符使用小写字母

    public JsonResult Search(string searchText) {
        ....
        return Json(new { name = UserNames, imageUrl = ImageUrls }, JsonRequestBehavior.AllowGet);
    }
    
于 2012-06-03T15:59:54.293 回答